题意:略;

首先知道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. 15.1-uC/OS-III资源管理(锁调度器)

    1.大部分独占资源的方法都是创建临界段:1) 关中断方式2) 锁调度器方式3) 信号量方式4) mutex方式 2.独占共享资源的最快和最简单方法是关中断 然而,关/开中断是和CPU相关的操作,其相关 ...

  2. dict()的另一种用法

    先了解两个内容: 定义字典的两种方法: d1 = { ‘name’ :‘zzl’} #方法一 d2 = dict( name = 'zzl' ) #方法二 2. __dict__()方法不记录类的属性 ...

  3. #学号 20175201张驰 《Java程序设计》第1周学习总结

    教材学习内容总结 第一章 ·第一章的内容相对简单,我并未遇到大的困难. ·1.Java特点:语法简单.面向对象.与平台无关.动态. ·2.关于编写源文件:源文件名字必须与public类的名字相同:保存 ...

  4. 查看文件内容 cat , tac

    cat  文件名字tac  文件名字  --  倒序查看文件内容

  5. MyBatis中调用存储过程和函数

    一.调用存储过程 1.首先在数据库中定义存储过程,定义的存储过程的代码如下: //定义存储过程 create or replace procedure pag_add(p1 varchar2,p2 v ...

  6. Python之socket_tcp

    1.1socket编程之tcp编程 """ socket类型 sock_stream 面向连接的流套接字,默认值 tcp协议 sock_dgram 无连接的数据报文套接字 ...

  7. noip2016海港

    题目描述 Description 小K是一个海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客. 小K对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只 ...

  8. week_one-python基础 基本语法、流程控制

    金角大王的紫金葫芦,python开发环境介绍链接:http://list.youku.com/albumlist/show/id_28961509.html # Author:larlly pytho ...

  9. Linux configure,make,make install

    https://www.cnblogs.com/tinywan/p/7230039.html https://www.sohu.com/a/191735643_505857 https://blog. ...

  10. Bugku-CTF之输入密码查看flag

    Day14 输入密码查看flag http://123.206.87.240:8002/baopo/