KPSUM - The Sum

  One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that he placed signs + and - between every pair of adjacent digits alternately. Now he wants to find the value of the expression he has made. Help him.

  For example, if N=12 then +1 -2 +3 -4 +5 -6 +7 -8 +9 -1+0 -1+1 -1+2 = 5

Input

  Each line contains one integer number N (1≤ N ≤ 1015). Last line contains 0 and shouldn't be processed. Number of lines in the input does not exceed 40.

Output

  For every line in the input write the answer on a separate line.

Example

Input:
12
0 Output:
5   数位DP,表示一开始就打错了,然后乱改了一天,终于改对了。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long Quick_pow(long long a,int k){
long long ret=;
while(k){if(k&)ret*=a;a=a*a;k>>=;}
return ret;
} long long Query(long long sum,int lena,int lenb){
if(lena%){
if(lenb%)
return sum*Quick_pow(,lenb)+*Quick_pow(,lenb-);
else
return *Quick_pow(,lenb-);
}
else{
if(lenb%)
return *Quick_pow(,lenb-);
else
return sum*Quick_pow(,lenb);
}
}
int st[],cnt;
int me[]={,,-,,-,,-,,-,,-};
long long Calc(long long a,int b){
if(a==)return me[b];
int flag=-;
long long ret=;
for(int i=;i<=b;i++){
long long x=a*+i;cnt=;
while(x){st[++cnt]=x%;x/=;}
while(cnt){
ret+=flag*st[cnt--];
flag=-flag;
}
}
return ret;
}
int num[],tot;
long long mem[];
int main(){
mem[]=;
for(int i=;i<=;i++){
for(int j=;j<=;j++)
mem[i]+=Query(-j,,i-);
mem[i]+=mem[i-];
} long long n;
while(~scanf("%lld",&n)&&n){
long long ret=,x=n,sum=;tot=;
while(x){num[++tot]=x%;x/=;}
for(int i=tot;i>;i--){
if(!num[i])continue;
if(i!=tot)ret+=Query(sum,tot-i+,i-);
else ret+=mem[i-];
for(int j=;j<num[i];j++)
ret+=Query(sum+Quick_pow(-,tot-i+)*j,tot-i+,i-);
sum+=Quick_pow(-,tot-i+)*num[i];
}
ret+=Calc(n/,n%);
printf("%lld\n",ret);
}
return ;
}

数位DP:SPOJ KPSUM - The Sum的更多相关文章

  1. [数位dp] spoj 10738 Ra-One Numbers

    题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...

  2. SPOJ KPSUM ★(数位DP)

    题意 将1~N(1<=N<=10^15)写在纸上,然后在相邻的数字间交替插入+和-,求最后的结果.例如当N为12时,答案为:+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2=5 ...

  3. 【SPOJ 1182】 SORTBIT - Sorted bit squence (数位DP)

    SORTBIT - Sorted bit squence no tags Let's consider the 32 bit representation of all integers i from ...

  4. 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)

    BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...

  5. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  6. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  7. SPOJ BALNUM Balanced Numbers (数位dp)

    题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...

  8. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

随机推荐

  1. mvc Action上面加 [HttpPost]

    mvc  Action上面加 [HttpPost]  意思就是这个action只能响应post请求. 如果发get请求这里是没有响应的

  2. 关于URL 解码, 编码

    由于近期客户需要用到CA认证,此CA认证采用的是URL方式出传值 使用指定的编码对象将 URL 编码的字符串转换为已解码的字符串. 编码个人理解就是将某字符串以某种方式储存起来,而解码则以其编码格式得 ...

  3. ASP.NET Boilerplate 工作单元

    从上往下说起,框架使用castle拦截器,拦截实现了IApplication.IRepository接口的所有方法,和使用了UnitOfWork 特性的方法,代码如下 internal class U ...

  4. TypeScript笔记[5]泛型+Dictionary 转

    TypeScript笔记[5]泛型   在C++.C#.Java等主流编程语言中,一般对泛型编程提供了支持.合理利用泛型,可以提高开发效率.提升代码质量. 例如在C++编程语言中,常常利用下面的结构表 ...

  5. 将MVC中的Controllers、Model和View分别放到单独的项目中

    Model: 新建-项目-Windows-类库 MVCTest.Model Controller:新建-项目-Windows-控制台应用程序 MVCTest.Bussiness Views:新建-项目 ...

  6. Simple screenshot that explains the singleton invocation.

    Here is the code: /* Some class,such as a config file,need to be only one.So we need to control the ...

  7. 用PHP实现一个高效安全的ftp服务器(一)

    摘要: 本文主要阐述使用PHP的swoole扩展实现ftp服务器,同时扩展ftp服务器个性化功能和安全性.真正实现一个自己完全掌控的ftp服务器,可以个性化定制的ftp服务器. 正文: FTP服务器想 ...

  8. iOS打包ipa安装包的流程

    应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...

  9. 理解pkg-config工具

    你在 Unix 或 Linux 下开发过软件吗?写完一个程序,编译运行完全正常,在你本机上工作得好好的,你放到源代码管理系统中.然后,告诉你的同事说,你可以取下来用了.这时,你长长的出了一口气,几天的 ...

  10. 全部与精简切换显示jQuery实例教程

    下面是某网站上的一个品牌列表展示效果,用户进入页面时,品牌列表默认是精简显示的(即不完整的品牌列表)效果如下图所示: 用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌.单击“显示全部品牌 ...