博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1140——How Many Zeroes?(数位dp)
阅读量:2343 次
发布时间:2019-05-10

本文共 1660 字,大约阅读时间需要 5 分钟。

Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

Input

Input starts with an integer T (≤ 11000), denoting the number of test cases.

Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

Output

For each case, print the case number and the number of zeroes written down by Jimmy.

Sample Input

5
10 11
100 200
0 500
1234567890 2345678901
0 4294967295
Output for Sample Input
Case 1: 1
Case 2: 22
Case 3: 92
Case 4: 987654304
Case 5: 3825876150

求m到n的数共有多少个0

要注意会出现01,001,0001这些前导0的情况,用first判断首位是否是0
lightoj的64位输入输出差点把我又坑一次

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 10000005#define Mod 10001using namespace std;int dight[30];long long dp[20][20];long long dfs(int pos,int s,bool limit,int first){ if(pos==0) { if(first) return 1; return s; } if(!limit&&dp[pos][s]!=-1&&!first) return dp[pos][s]; int end; long long ret=0; if(limit) end=dight[pos]; else end=9; for(int d=0; d<=end; ++d) { if(first) ret+=dfs(pos-1,0,limit&&d==end,first&&d==0); else { if(d==0) ret+=dfs(pos-1,s+1,limit&&d==end,0); else ret+=dfs(pos-1,s,limit&&d==end,0); } } if(!limit&&!first) dp[pos][s]=ret; return ret;}long long solve(long long a){ memset(dight,0,sizeof(dight)); int cnt=1; while(a!=0) { dight[cnt++]=a%10; a/=10; } return dfs(cnt-1,0,1,1);}int main(){ memset(dp,-1,sizeof(dp)); int t,cnt=1; scanf("%d",&t); while(t--) { long long x,y; scanf("%lld%lld",&x,&y); printf("Case %d: %lld\n",cnt++,solve(y)-solve(x-1)); } return 0;}

转载地址:http://fvcvb.baihongyu.com/

你可能感兴趣的文章
程序员找工作的个人经验教训以及注意事项
查看>>
2019 编程语言排行榜:Java、Python 龙争虎斗!谁又屹立不倒
查看>>
拥有10年编程经验的你,为什么还一直停留在原地
查看>>
Flask vs Django,Python Web开发用哪个框架更好
查看>>
用Python制作动态二维码,一行代码就做到了
查看>>
Python说:常见的数据分析库有哪些
查看>>
Python教程:Python数据类型之字典
查看>>
Python基础教程:python的数据类型
查看>>
Python学习教程:另辟蹊径,appium抓取app应用数据了解一下
查看>>
周董新歌《说好不哭》上线,20W评论,歌迷都说了些啥
查看>>
Python学习教程:用Python进行金融市场文本数据的情感计算
查看>>
Python爬虫:python获取各种街拍美图
查看>>
爬虫工程师是干什么的?你真的知道吗?
查看>>
写给那些想学Python的人,建议收藏后细看
查看>>
数据全裸时代,你的隐私有多容易获取?
查看>>
分析http代理报错问题
查看>>
Python编程学习笔记 - 列表的各种姿势
查看>>
Python学习教程:Python入门笔记整理
查看>>
天了噜,居然用Python查到了女神的姓名
查看>>
常用排序算法总结
查看>>