题意:略;

首先知道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. 最全的MonkeyRunner自动化测试从入门到精通(10)

    三.MonkeyRunner复杂的功能开始学习 (1)获取APK文件中ID的两种方式 Monkeyrunner的环境已经搭建完成,现在对Monkeyrunner做一个简介. Monkeyrunner工 ...

  2. 学习gstreamer

    1. 对gst 的框架认识. 第一篇文章有结构图说明,清楚易懂:第二篇文章介绍了gst的简单使用 http://www.cnblogs.com/jingzhishen/p/3709639.html h ...

  3. Redis入门到高可用(十五)—— HyperLogLog

    一.简介 二.API Demo 三.使用经验

  4. java框架之SpringCloud(1)-微服务及SpringCloud介绍

    微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...

  5. java框架之SpringCloud(3)-Eureka服务注册与发现

    在上一章节完成了一个简单的微服务案例,下面就通过在这个案例的基础上集成 Eureka 来学习 Eureka. 介绍 概述 Eureka 是 Netflix 的一个子模块,也是核心模块之一.Eureka ...

  6. qrcode插件生成二维码

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  7. redis安装及错误排查

    安装: 1.cd /usr/redis   //redis目录作为安装目录,没有自行创建 2.tar xzf  redis-4.0.6.tar.gz 3. cd redis-4.0.6 4.make ...

  8. oracle导出导入指定表

    从源数据库导出: exp user1/pwd@server1/orcl file=c:\temp\exp.dmp tables=(table1, table2) 导入到目标数据库: imp user2 ...

  9. Linux基础命令---lprm删除打印任务

    lprm lprm指令用来删除当前打印队列上的任务,如果没有指定,那么就删除当前打印任务.您可以指定一个或多个职务ID编号来取消这些职务,或者使用选项”-”取消所有作业. 此命令的适用范围:RedHa ...

  10. 《CSS世界》读书笔记(六)

    <!-- <CSS世界> 张鑫旭著 --> min-width/max-width和min-height/max-height min-width/max-width出现的场景 ...