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. asp.net 事件模型

    asp.net的原始设计构想,就是要让开发人员能够像 VB 开发工具那样,可以使用事件驱动式程序开发模式 (Event-Driven Programming Model) 的方法来开发网页与应用程序, ...

  2. 实用脚本 - - addLoadEvent 页面加载完毕执行函数

    function addLoadEvent(func){ var oldonload = window.onload; if(typeof window.onload != "functio ...

  3. Simple screenshot that explains the non-static invocation.

    Here is the code: /* Instance invocation in the memory: */ package kju.obj; import static kju.print. ...

  4. oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char

    1.number类型在库中可能存在null这种数据 判断是否为空时要用如下: IF(nvl(:NEW.BACAH,0) <>0) 不能用IF(BACAH IS NOT NULL) 2. 2 ...

  5. index ffs、index fs原理考究-1109

    h2 { margin-top: 0.46cm; margin-bottom: 0.46cm; direction: ltr; line-height: 173%; text-align: justi ...

  6. WHU 1572 Cyy and Fzz (AC自动机 dp )

    题意: 给出n个串,求任意长度为m的字符串包含串的个数的期望.(n<=8,m<=14,给定串的长度不超过12). Solution: 首先可以想到应该用概率DP,我们需要至少3维,dp[i ...

  7. phpcms源码跟踪(1)

    本次跟踪解决几个问题: 1.缓存文件从哪里来,过程中被删除了怎么办 2.模板html是如何被引入的 进入首页时,通过最初的调用,进入控制器\phpcms\modules\content\index.p ...

  8. yum安装ftp服务器

    1.安装vsftp,本文采用yum安装: #yum install vsftpd 2.安装后运行: # service vsftpd restart Shutting downvsftpd:      ...

  9. 【转】 C语言自增自减运算符深入剖析

    转自:http://bbs.csdn.net/topics/330189207 C语言的自增++,自减--运算符对于初学者来说一直都是个难题,甚至很多老手也会产生困惑,最近我在网上看到一个问题:#in ...

  10. C /CLI思辨录[阅读记录]

    C /CLI思辨录之拷贝构造函数(避免多个实例在相同的堆对象的错误处理) http://west263.com/info/html/chengxusheji/C-C--/20080224/9247.h ...