题意:略;

首先知道10,20,......100,200,1000的前面的符号都是负号。

举具体例子:221时,计算过程为

000-009,     010-019,   020-029......,090-099;    100-199;   200-209, 210-219,   220-221;

首先第1位能取0,1,2(2是上界)。先看选1时,这时没有前倒0,没到上界。因此能直接计算,一共100项,百位和为0,再算出后缀。然后第1位取0

第1位取0:第二位取1-9,能直接计算。观察到只要没有前倒0或者没到上限就能直接算从结果。类似第一位取2,第二位取0-1,时因为没到上限,直接算。

如果一直到了最后一位都有前倒0或上限,即000-009,220-221。就暴力计算,一直有上限的情况要传递前缀。

因此dfs的参数有5个,当前位,有无前倒0,是否取上限,前缀位数,前缀。

代码v1.1

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <unordered_set>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=*SZ,APB=,INF=0x7FFFFFFF,mod=;
lon n,arr[SZ],sz; void release()
{ } lon func(lon x)
{
lon res=,t=;
for(;x;x/=)res+=x%*t,t=-t;
res*=-t;
return res;//+开头
} lon dfs(lon pos,lon lim,lon lead,lon pre,lon fix)
{
lon up=lim?arr[pos]:,res=;
if(!lead&&!lim)
{
lon tot=(pre+(sz-pos))&;
if(!tot)
{
if(!(pre&))res=;
else res=*((lon)pow(,sz-pos-));
}
else
{
res=(lon)pow(,sz-pos)/;
}
return res;
}
else if(pos==sz-)
{
if(lead)for(lon i=;i<=up;++i)res+=(i&?:-)*i;
else if(lim)
{
for(lon i=;i<=up;++i)
{
if(pre&)res+=func(fix*+i);
else res+=(i&?:-)*func(fix*+i);
}
if(pre&)res=-res;
}
return res;
}
else
{
for(lon i=;i<=up;++i)
{
lon nlim=lim&&i==up,nlead=lead&&i==;
lon cur=;
if(!nlim&&lim||(!nlead&&lead&&pos))
{
lon tot=pre+(sz-pos);
if(!(tot&))
{
cur-=func(fix*+i)*pow(,sz-pos-);
}
}
cur+=dfs(pos+,nlim,nlead,nlead?pre:pre+,fix*+i);
res+=cur;
}
return res;
}
} lon calc(lon x)
{
sz=;
for(;x;x/=)arr[sz++]=x%;
reverse(arr,arr+sz);
return dfs(,,,,);
} void init()
{
cout<<calc(n)<<endl;
} void work()
{ } int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\2.txt","r",stdin);
//freopen("d:\\3.txt","w",stdout);
lon casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(lon time=1;time<=casenum;++time)
for(lon time=;cin>>n,n;++time)
{
init();
work();
release();
}
return ;
}

v1.0

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <unordered_set>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=*SZ,APB=,INF=0x7FFFFFFF,mod=;
lon n,arr[SZ],sz; void release()
{ } lon func(lon x)
{
lon res=,t=;
for(;x;x/=)res+=x%*t,t=-t;
res*=-t;
return res;//+开头
} lon dfs(lon pos,lon lim,lon lead,lon pre,lon fix)
{
//cout<<pos<<endl;
lon up=lim?arr[pos]:,res=;
if(pos==sz)return ;
else if(pos==sz-)
{
if(!fix)for(lon i=;i<=up;++i)res+=(i&?:-)*i;
else if(lim)
{
for(lon i=;i<=up;++i)
{
if(pre&)res+=func(fix*+i);
else res+=(i&?:-)*func(fix*+i);
//cout<<fix*10+i<<endl;
}
if(pre&)res=-res;
//cout<<res<<endl;
}
else
{
if(!(pre&))
{
for(lon i=;i<=up;++i)res+=(i&?:-)*i;
//cout<<res<<endl;
}
else for(lon i=;i<=up;++i)res+=i;
}
return res;
}
else if(!lead&&!lim)
{
lon tot=(pre+(sz-pos))&;
if(!tot)
{
if(!(pre&))res=;
else res=*((lon)pow(,sz-pos-));
}
else
{
res=(lon)pow(,sz-pos)/;
}
return res;
}
else
{
for(lon i=;i<=up;++i)
{
lon nlim=lim&&i==up,nlead=lead&&i==;
lon cur=;
if(!nlim&&lim||(!nlead&&lead&&pos))
{
//if(pre&1);
//else
{
lon tot=pre+(sz-pos);
if(tot&)
{ }
else
{
cur-=func(fix*+i)*pow(,sz-pos-);
} }
}
//cout<<"i: "<<pos<<" "<<i<<" "<<cur<<endl;
cur+=dfs(pos+,nlim,nlead,nlead?pre:pre+,fix*+i);
res+=cur;
//cout<<"i: "<<pos<<" "<<i<<" "<<cur<<" "<<(!nlead&&lead)<<endl;
}
return res;
}
} lon calc(lon x)
{
sz=;
for(;x;x/=)arr[sz++]=x%;
reverse(arr,arr+sz);
return dfs(,,,,);
} void init()
{
cout<<calc(n)<<endl;
} void work()
{ } int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\2.txt","r",stdin);
//freopen("d:\\3.txt","w",stdout);
lon casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(lon time=1;time<=casenum;++time)
for(lon time=;cin>>n,n;++time)
{
init();
work();
release();
}
return ;
}

spoj1433 KPSUM的更多相关文章

  1. 【数位统计】之【spoj1433 KPSUM】

    [spoj1433]KPSUM 来源 高逸涵<数位计数问题解法研究> 由于自己的数位计数类的问题实在太差了,所以把例2用markdown抄写并补充了一遍. 题意 将写在纸上,然后在相邻的数 ...

  2. BZOJ 1236: SPOJ1433 KPSUM

    Description 用+-号连接1-n所有数字的数位,问结果是多少. Sol 数位DP. \(f[i][j][0/1][0/1]\) 表示长度为 \(i\) 的数字,开头数字是 \(j\) ,是否 ...

  3. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  4. 数位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 ...

  5. 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 ...

  6. SDOI2016R1(不是解题报告)

    话说洗澡的时候想了一堆要说的,坐到电脑前反而不知所措了-- 序章 听学长说他们都是省选一周前才停的课.然而我们这届--自聪哥韩大他们在省选两周前悄悄跑路后(据说班主任非常支持),信息小组内部一呼百应, ...

  7. 题解[SCOI2009]粉刷匠 难度:省选/NOI-

    Description windy有 N 条木板需要被粉刷.每条木板被分为 M 个格子.每个格子要被刷成红色或蓝色.windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色.每个格子最多 ...

随机推荐

  1. Chrome扩展程序——TabCopy:一键复制网页标题和网址

    Chrome扩展程序——TabCopy:一键复制网页标题和网址 - Erik_ly的博客 - CSDN博客 https://blog.csdn.net/u012318074/article/detai ...

  2. 11.1-uC/OS-III就绪列表

    准备好运行的任务被放到就绪列表中, 如图6-1.就绪列表是一个数组( OSRdyList[]),它一共有OS_CFG_PRIO_MAX条记录,记录的数据类型为OS_RDY_LIST(见OS.H).就绪 ...

  3. align-item 和 align-content的区别

    align-content 和 align-items  : 1:共同点:它们对齐方向为交叉轴 2:不同点:align-content 应用于为 多行   而 align-items:应用于单行.

  4. cocos2d-x JS 富文本

    var str1 = "兑换成功后,系统会生成“";var str2 = "红包兑换码";var str3 = "”,请复制该兑换码,并粘贴在&quo ...

  5. OAuth2认证和授权:ClientCredentials认证

    1:创建授权服务器项目:AuthorizationServer,添加包:IdentityServer4 2:创建资源服务器项目:ResourcesServer,添加包:IdentityServer4. ...

  6. Linux平台 Oracle 18c RAC安装Part2:GI配置

    三.GI(Grid Infrastructure)安装 3.1 解压GI的安装包 3.2 安装配置Xmanager软件 3.3 共享存储LUN的赋权 3.4 使用Xmanager图形化界面配置GI 3 ...

  7. phpstorm----------phpstorm2017基本使用

    1.关闭2017版本的,函数参数提示.关闭方式如下: 2.如何设置代码里面的变量等号对齐,和key => value 对齐     ctrl+alt+l 3.修改PHP文件类创建的默认注释 4. ...

  8. tf.InteractiveSession()和tf.Session()

    tf.InteractiveSession()适合用于python交互环境 tf.Session()适合用于源代码中 1.tf.InteractiveSession() 直接用eval()就可以直接获 ...

  9. eclipse无法断点调试JDK源码的问题

    最近换了新版的eclipse,在jdk源码里面,打断点发现无法进入源码调试,程序直接跳过,已查资料发现自己eclipse配置的是jre环境的. 此处要配成jdk目录才有效 打开preferences, ...

  10. random模块写的验证码

    import randomabc=''for i in range(4):    a=random.randrange(0,4)    if i != a:        b=chr(random.r ...