数位DP:SPOJ KPSUM - The Sum
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的更多相关文章
- [数位dp] spoj 10738 Ra-One Numbers
题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...
- 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 ...
- 【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 ...
- 【SPOJ 2319】 BIGSEQ - Sequence (数位DP+高精度)
BIGSEQ - Sequence You are given the sequence of all K-digit binary numbers: 0, 1,..., 2K-1. You need ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- 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 ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
随机推荐
- (转)基于PHP的cURL快速入门
1. 原文:基于PHP的cURL快速入门 英文原文:http://net.tutsplus.com/tutorial ... for-mastering-curl/ 原文作者:Burak Guzel ...
- eclipse(myEclipse) 配置maven项目
工作中在myeclipse中导入maven工程后,在pom.xml文件目录执行了mvn eclipse:eclipse 后,发现项目中缺少"Maven Dependencies"目 ...
- eclipse alt + '/' not working.
searching for google,I observed that the 'content assist' shortcut key was take placed with 'ctrl + ...
- 【转】iOS开发6:UIActionSheet与UIAlertView
原文: http://my.oschina.net/plumsoft/blog/42763 iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择 ...
- [转]dos命令 cd命令使用说明[图文说明]
Cddir(change directory,可以缩写为cd),其功能是显示当前目录的名称,或更改当前的目录. 应用时公带一个驱动器号(如: cd c:)在命令行cmd中输入 cd /? 可显示帮助信 ...
- SGU 164.Airline(结论题)
时间限制:0.25s 空间限制:4M 题意: 在n(1<=n<=200)个点的无向完全图中,有m种不同的边.现在从中选择不超过(m+1)/2种边,使得任意两点可以通过不超过3条边互相到达. ...
- SGU 188.Factory guard
模拟 code #include <iostream> #include <cstdio> #define LEN 1000 using namespace std; int ...
- 简单高效读写修改整个文本Slurp
语法: use File::Slurp; #标量环境下一次读取所有文本内容到标量中. my $text = read_file( 'filename' ) ; # 读取文本的所有行到数组中. my ...
- windowsphone 中CollectionViewSource和ObservableCollection的使用
功能描述:一级菜单省份 联动显示省份下的城市 直接上代码 public class City { public string Num { get; set; } public string Name ...
- js 代码记录
window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...