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. 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs

    在一个新的项目上需要实现在线视频播放,原本打算借助优酷的视频存储和播放,但是发现这个需要用户注册优酷账户,严重影响用户体验,于是这个方案被毙掉了.于是开始了自己开发一个在线播放器的想法,当然尽量使用已 ...

  2. VBA开发中的前绑定与后绑定

    凡是能用createobject创建的对象,都可以在引用相对应的运行库(library)文件之后在对象浏览器中得到它的方法.属性.枚举和事件列表,比如Shell.Application对象在Shell ...

  3. ASP.NET 打包下载文件

    使用的类库为:ICSharpCode.SharpZipLib.dll 一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异: using ICSharpCode.SharpZipLib.Zip; ...

  4. Axiom3D学习日记 5.Frame Listeners, and Input Handling

    Frame Listeners In Ogre's C++, we would register a class to receive notification before and after a ...

  5. HTML5 文件域+FileReader 分段读取文件并上传(八)-WebSocket

    一.同时上传多个文件处理 HTML: <div class="container"> <div class="panel panel-default&q ...

  6. javascript基础学习(八)

    javascript之日期对象 学习要点: 日期对象 将日期对象转换为字符串 将日期对象中的日期和时间转换为字符串 日期对象中的日期 日期对象中的时间 设置日期对象中的日期 设置日期对象中的时间 与毫 ...

  7. VS 编程常见错误及解决方法

    1. VS2013 无法打开包括文件:“cv.h"等一些头文件 解决方法: cv.h是build\include文件夹下的头文件,所在文件夹位置是D:\Program Files (x86) ...

  8. 一种实现C++反射功能的想法(二)

    在介绍我的思路前, 让我们准备下预备知识 C++是怎么实现类函数的绑定的. 我们知道类的非静态成员函数是存储在全局区, 并在内存中只保存一份副本. 我们调用非静态成员函数是通过类对象进行调用. 那么如 ...

  9. SGU 157.Patience

    简单的搜索,在n>10时,要打表 code: #include<stdio.h> #include<string.h> #include<algorithm> ...

  10. windows live writer 下载及安装

    windowslive writer下载地址: http://www.microsoft.com/en-us/download/details.aspx?id=8621(不知为啥,这里我无法下载)或 ...