题意:略;

首先知道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. 转:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述

    原文地址:ArcGIS API For JavaScript官方文档(二十)之图形和要素图层——①Graphics概述 ArcGIS JavaScript API允许在地图上绘制graphic(图形) ...

  2. vue.js 之 watch 详解

    接我上篇博客的例子: 在上面代码中,当我们修改 firstName 或 lastName 后,watch 监听每次修改变化的新值,然后计算输出 fullName:此时 watch 的一个缺点是,最初绑 ...

  3. A标签中 href 和 onclick用法、区别、优先级别

    (内容摘自:https://blog.csdn.net/chenchunlin526/article/details/77346049) Html A标签中 href 和 onclick 同时使用的问 ...

  4. 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序

    npm install  webpack-dev-server --save

  5. python 使用函数参数注解

    使用函数参数注解是一个很好的办法,它能提示程序员应该怎样正确使用这个函数. 函数注解只存储在函数的annotations 属性中

  6. python_study-1

    # Author:larlly'''函数1.在Python交互式命令行下,可以直接输入代码,然后执行,并立刻得到结果.2.文本编辑器推荐俩款 http://www.sublimetext.com/ h ...

  7. Python+OpenCV图像处理(十四)—— 直线检测

    简介: 1.霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法.主要用来从图像中分离出具有某种相同特征的几何形状(如,直线 ...

  8. CAN control

    2019/4/23--10:14 E_BSW_NWK_TRIGGER_SOURCE_KICK_MOTION_CMD SCI_NwkButton_GetPeriodicSignals case 6:   ...

  9. Python Kivy writes / read the file on the SD card

    Path to SD card from jnius import autoclass # SDcard Android # Get path to SD card Android try: Envi ...

  10. kruscal重构树略解

    我们先看一道题:Luogu P4197 Peaks 这道题珂以用启发式合并+主席树来做 那么强制在线呢?(bzoj 3551 [ONTAK2010]Peaks加强版) 离线做法就不行了 我们就要用一个 ...