数位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个的数的 ...
随机推荐
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)
// VacationRequest.java /** * author : 冯孟活 ^_^ * dates : 2015年9月1日 下午10:32:58 * class : 演示简单的公司请假流程 ...
- (ternary operator)三元运算符.
ternary operator: advantage: make a terse simple conditional assignment statement of if-then-else. d ...
- angularjs 利用filter进行表单查询及分页查询
页面: <div> <input style="width:90%;margin-left:5px;margin-right:5px;" class=" ...
- WINDOWS批处理命令使用大全
来源:http://www.942dn.com就是爱电脑网 WINDOWS批处理命令使用大全 批处理,也称为批处理脚本,英文译为BATCH,批处理文件后缀BAT就取的前三个字母.它的构成没有固定格式, ...
- iOS 原生二维码扫描(可限制扫描区域)
篇文章的主要原因不是展示如何使用 AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围.(因为默认的是全屏扫描) 项目遇到扫描二维码的功能需求,这里我放弃了使用三方库,而采用了 ...
- IOS应用程序生命周期&启动周期函数
—程序的生命周期 a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程 b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...
- OC - 31.通过封装的自定义布局快速实现商品展示
概述 实现效果 设计思路 采用MVC架构,即模型—视图-控制器架构 使用MJExtension框架实现字典转模型 使用MJRefresh框架实现上拉和下拉刷新 上拉刷新,加载新的数据 下拉刷新,加载更 ...
- Java 基础(一)
Java不只是一种语言,更是一个完整的平台,有一个庞大的库,其中包含了很多可重用的代码和一个提供诸如安全性.跨操作系统的可移植性以及自动垃圾收集等服务的执行环境. javaSE: 整个java技术的核 ...
- nim
上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在玩一个游戏,每次寻找边长为x的正方形,其中每个格子必须为黑色,然后将这些格子染白. 如果谁不能操作了,那么那个人就输了. ...