51nod 1042 数位dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042
两个数a,b(1 <= a <= b <= 10^18)
输出共10行,分别是0-9出现的次数
10 19
1
11
1
1
1
1
1
1
1
1
经典的数位dp题目,但是这个'0'真是搞得我恶心,第一次见是在玲珑某次比赛,那次一直怼这个最后爆零- -
dp[i]表示[0,10
i
-1]之间所有的数里面0-9出现的次数,有dp[i]=10*dp[i-1]+pow(10,i-1),显然1-9的次数的一样的,0得话,
因为没有以零开头的多位数,所以这里面是多计算了一部分'0'的,计算时遇到0就要想办法减去他才行。
假如[0,999],多计算的就是100+10+1个零,对应的是001,002.....099,100是最高位,10是次高位,依次递推。
假如要计算f(2049,0),第一次 s+=dp[3]*2;
这两个dp[3]一个是加的 [0,999]一个是[1000,1999],但是我们只去掉一次就好了因为在[1000,1999]间那些零反而是我们需要的,就是这一点我糊涂好久。
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
LL dp[], zero[] = { ,, };
LL qpow(LL a,LL b,LL r=){for(;b;b>>=,a=a*a)if(b&)r=r*a;return r;}
void init()
{
dp[]=;
for(LL i=;i<;++i)
dp[i]=*dp[i-]+qpow(,i-);
}
LL f(LL N,LL digit)
{
if(N==) {return digit?:;}
int len=log(N+0.05)/log()+;
LL s=,nx=N;
for(int i=len;i>=;--i)
{
LL mul=qpow(,i-);
s+=dp[i-]*(N/mul);
{
if(N/mul->=digit) s+=qpow(,i-);
if(N/mul==digit) s+=N%mul+;
}
N%=mul;
//if(!digit) cout << "s=" << s << endl;
}
if (!digit) // 删除前缀是0的结果
{
LL m = ;
while (nx)
{
s -= m;
m *= ;
nx = nx / ;
}
}
//if (!digit) cout <<"s="<< s << endl;
return s;
}
int main()
{
init();
LL a,b,x;
cin >> a >> b;
for(x=;x<=;++x)
{
cout<<f(b,x)-f(a-,x)<<endl;
}
return ;
}
这是从新学习后自己写的代码
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL f[]={,};
LL p10[]={,};
LL zero[]={,};
LL bit[];
void init(){
for(int i=;i<=;++i) p10[i]=p10[i-]*;
for(int i=;i<=;++i) zero[i]=zero[i-]*+;
for(int i=;i<=;++i) f[i]=f[i-]*+p10[i-];
}
LL cal(LL N,int x){
int len=;
while(N){
bit[len++]=N%;
N/=;
}
bit[len]=-;
LL ans=,tot=;
for(int i=len-;i>=;--i){
ans+=f[i]*bit[i];
if(!x && i==len-) ans-=(LL)(zero[i]);
if(bit[i]>x && ((x==&&i==len-)==) ) ans+=p10[i];
ans+=p10[i]*bit[i]*tot;
if(bit[i]==x) tot++;
}
return ans;
}
int main(){
LL l,r,n,i,j,k;
init();
while(scanf("%lld%lld",&l,&r)==){
for(i=;i<;++i)
printf("%lld\n",cal(r+,i)-cal(l,i));
}
return ;
}
51nod 1042 数位dp的更多相关文章
- 51nod 1009 数位dp入门
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 1009 数字1的数量 基准时间限制:1 秒 空间限制:13107 ...
- 51 Nod 1042 数位dp
1042 数字0-9的数量 1 秒 131,072 KB 10 分 2 级题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次(10,11,12,13,14,1 ...
- 51nod 1043 数位dp
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 1043 幸运号码 基准时间限制:1 秒 空间限制:131072 ...
- 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 基准时间限制:1 秒 空间限制:131072 KB 给 ...
- 51NOD 1623 完美消除 数位DP
题目描述: 定义数的消除操作为选定[L,R,x],如果数的第L到第R位上的数字都大于等于x,并且这些数都相等,那么该操作是合法的(从低位到高位编号,个位是第一位,百位是第二位……),然后将这些位数上的 ...
- 51nod 1009 数字1的数量(数位dp模板)
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数. 例如:n = 12,包含了5个1.1,10,12共包含3个1,11包含2个1,总共5个1. 数位dp的模板题 ...
- 51Nod 1009 数字1的个数 | 数位DP
题意: 小于等于n的所有数中1的出现次数 分析: 数位DP 预处理dp[i][j]存 从1~以j开头的i位数中有几个1,那么转移方程为: if(j == 1) dp[i][j] = dp[i-1][9 ...
- 51nod1043(数位dp)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 题意:中文题诶- 思路:数位dp 我们用dp[i][j ...
- 1043 幸运号码 数位DP
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 设dp[i][j]表示前i位数中,i位数的和为j时的所有情况. 转 ...
随机推荐
- springmvc控制器controller单例问题
springmvc controller默认的是单例singleton的,具体可以查看注解scope可以一目了然. 单例的原因有二: 1.为了性能. 2.不需要多例. 1.这个不用废话了,单例不用每次 ...
- 【Linux command reference】
ubuntu16.04安装中文输入法: https://blog.csdn.net/singleyellow/article/details/77448246 ubuntu16.04 用vi编辑代码, ...
- git "Could not read from remote repository.Please make&n
git "Could not read from remote repository.Please make sure you have the correct access rights. ...
- windows下安装Composer提示缺少openssl的解决方法
在Windows环境下安装Composer(注:Composer要求PHP版本在5.3.2+),你可能会遇到这种安装失败的情况:出错信息是 "The openssl extension is ...
- rest_framake之视图
开始,先放大招 一 最原始的写法 前戏之序列化 class AuthorSerializer(serializers.ModelSerializer): class Meta: model = mo ...
- 重读C库之宏定义
1.如何编写头文件.h? //file--func1.h #ifndef __FUNC1_H //__func1_h //可小写可大写 #define __FUNC1_H //__func1_h .. ...
- Android开发中string.xml文件的使用
为什么需要把应用中出现的文字单独存放在string.xml文中呢? 一:是为了国际化,Android建议将在屏幕上显示的文字定义在strings.xml中,如果今后需要进行国际化,比如我们开发的应用本 ...
- ubuntu16.04 tomcat7安装和编码修改(转发:https://blog.csdn.net/zl544434558/article/details/76735564)
有直接通过命令安装的,但是我还是喜欢把文件下载下来,然后自己配置. 1,下载tomcat7二进制文件 https://tomcat.apache.org/download-70.cgi 2,解压tom ...
- Spring 数据库连接池读取系统环境变量作为参数
原来是写在一个properties文件里面,后来项目要部署的的确太多了,每次更改不太方便,就想把这些固定不变的信息写在当地的环境变量里面 原先是这样的:引用的所有信息在jdbc.properties ...
- 每天学点Linux命令之grep 和 wc命令 ---(6/25)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...