How Many Zeroes? LightOJ
来源:互联网 发布:淘宝手机分期12期免息 编辑:程序博客网 时间:2024/05/21 23:46
题目:
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
Sample Output
Case 1: 1
Case 2: 22
Case 3: 92
Case 4: 987654304
Case 5: 3825876150
大意:
给你一个范围,求这个范围内的数字中出现0的次数
思路:
数位dp,注意按位dp时出现前导0
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn = 123456;int T;ll l,r,ans;ll a[25];ll dp[25][25];//dp[i][j]处理到第i位时前面出现了j个0ll dfs(int pos,bool limt,int cnt,bool is0)//is0表示前面是否一直为0{ if(!pos)//计算到最后一位 return is0?1:cnt;//前导0之类的算一个0 ll sum=0; if(!limt&&dp[pos][cnt]!=-1&&!is0) return dp[pos][cnt]; int k=(limt?a[pos]:9); for(int i=0;i<=k;i++) { if(is0) sum+=dfs(pos-1,limt&&(i==k),0,i==0); else sum+=dfs(pos-1,limt&&(i==k),cnt+(i==0),0); } if(!limt&&!is0) dp[pos][cnt]=sum; return sum;}ll solve(ll k){ int p=0; while(k) { a[++p]=k%10; k/=10; } return dfs(p,1,0,1);}int main(){ int no=0; scanf("%d",&T); while(T--) { scanf("%lld%lld",&l,&r); memset(dp,-1,sizeof(dp)); printf("Case %d: ",++no); printf("%lld\n",solve(r)-solve(l-1)); } return 0;}
阅读全文
0 0
- How Many Zeroes? LightOJ
- LightOJ 1140 How Many Zeroes?
- LightOJ 1140How Many Zeroes?
- LightOJ 1140 How Many Zeroes?
- LightOJ 1140 How Many Zeroes?(数位DP)
- lightoj 1140 How Many Zeroes? 数位dp
- LightOJ 1140 - How Many Zeroes? (数位dp)
- LightOJ - 1140 How Many Zeroes?(数位DP)
- lightoj 1140 - How Many Zeroes? 数位DP
- lightoj - 1140 How Many Zeroes? (数位dp)
- LightOJ 1140 How Many Zeroes? [数位DP]【动态规划】
- lightoj 1140 - How Many Zeroes?(数位DP)
- LightOJ 1140 How Many Zeroes?(数位dp 记忆化搜索方法)
- LightOJ 1077 How Many Points?
- light oj 1140 - How Many Zeroes?
- How Many Zeroes? (数位dp)
- lightoj 1077 - How Many Points? 最大公约数
- LightOJ 1077 How Many Points? (变种gcd)
- LeetCode——557. Reverse Words in a String III
- 初学者入门web前端 C#基础知识:数组与集合
- CentOS安装maven
- Udacity Deep Learning课程作业(五)
- H5面试题---如何阻止事件冒泡和默认事件
- How Many Zeroes? LightOJ
- MySQL数据库基础(一)
- 文本与序列的深度模型
- JTable不显示列标题的解决办法
- Android启动速度优化
- PX4飞控之自主起飞Takeoff控制逻辑
- jQuery异步请求ajax()之async详解
- 2017/8/21训练日记
- DEBUG o.m.d.connection:56 Closing connection connectionId{localValue:5}