spoj1433 KPSUM
题意:略;
首先知道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的更多相关文章
- 【数位统计】之【spoj1433 KPSUM】
[spoj1433]KPSUM 来源 高逸涵<数位计数问题解法研究> 由于自己的数位计数类的问题实在太差了,所以把例2用markdown抄写并补充了一遍. 题意 将写在纸上,然后在相邻的数 ...
- BZOJ 1236: SPOJ1433 KPSUM
Description 用+-号连接1-n所有数字的数位,问结果是多少. Sol 数位DP. \(f[i][j][0/1][0/1]\) 表示长度为 \(i\) 的数字,开头数字是 \(j\) ,是否 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 数位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 ...
- 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 ...
- SDOI2016R1(不是解题报告)
话说洗澡的时候想了一堆要说的,坐到电脑前反而不知所措了-- 序章 听学长说他们都是省选一周前才停的课.然而我们这届--自聪哥韩大他们在省选两周前悄悄跑路后(据说班主任非常支持),信息小组内部一呼百应, ...
- 题解[SCOI2009]粉刷匠 难度:省选/NOI-
Description windy有 N 条木板需要被粉刷.每条木板被分为 M 个格子.每个格子要被刷成红色或蓝色.windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色.每个格子最多 ...
随机推荐
- xargs实例
1. 当你使用rm命令去删除很多的文件时,你可能会得到错误信息:“/bin/rm Argument list too long – Linux”.这时可以用xargs来避免这个问题 find ~ -n ...
- Kubernetes实战(一):k8s v1.11.x v1.12.x 高可用安装
说明:部署的过程中请保证每个命令都有在相应的节点执行,并且执行成功,此文档已经帮助几十人(仅包含和我取得联系的)快速部署k8s高可用集群,文档不足之处也已更改,在部署过程中遇到问题请先检查是否遗忘某个 ...
- 18.0-uC/OS-III挂起内核对象
任务等待多个对象.然而, uC/OS-III只允许同时等待多个信号量和消息队列.换句话说,不能同时等待多个事件标志组或mutex. 1.任务可以同时等待多个信号量和消息队列. 任务接收到一个信号量或消 ...
- 问题: 揭秘Angualr2 书上问卷调查
npm install 初夏下面问题: 0 info it worked if it ends with ok1 verbose cli [ '/home/linux_ubuntu164/tools/ ...
- day04 一个简单的代码优化案例
import random punches = ['石头','剪刀','布'] computer_choice = random.choice(punches) user_choice = input ...
- 抓包工具 GOOGLE
chrome://net-internals/#events GOOGLE浏览器直接输入
- WebForm复合控件RadioButtonList、CheckBoxList、DropDownList
1.RadioButtonList 单选集合 -属性:RepeatDirection:Vertical (垂直排布)/Horizontal (横向排布) RepeatLayout:Table ...
- Applet程序组件与AJAX技术
Applet 定义 Applet是一种运行于Web客户端环境下的Java程序组件. 工作原理 Applet以代码的形式嵌入Web页面中,用标签<applet></applet> ...
- Linux基础命令---lpr打印文件
lpr lpr指令用来打印文件,如果没有指定文件名,那么从标准输入读取内容.CUPS提供了许多设置默认目标的方法.首先查询“LPDEST”和“PRINTER”环境变量.如果没有设置,则使用lpopti ...
- 我设计的电脑usb红外遥控键盘原理图
我设计的电脑usb红外遥控键盘,orcad原理图备份如下: