1140 - How Many Zeroes?
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
5 10 11 100 200 0 500 1234567890 2345678901 0 4294967295 |
Case 1: 1 Case 2: 22 Case 3: 92 Case 4: 987654304 Case 5: 3825876150 |
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
简单数位DP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[30];
ll dp[30][30];
ll dfs(int pos,int sta,int lead,int limit)
{if(pos==-1){return sta;}if(!limit&&!lead&&dp[pos][sta]!=-1) return dp[pos][sta];int up=limit?a[pos]:9;ll tmp=0;for(int i=0;i<=up;i++){if(lead==1&&i==0) //前导0{tmp+=dfs(pos-1,sta,lead,limit&&i==up);}else { if(i==0) tmp+=dfs(pos-1,sta+1,lead && i==0,limit&&i==up);elsetmp+=dfs(pos-1,sta,lead && i==0,limit&&i==up);}}if(!limit&&!lead) dp[pos][sta]=tmp;return tmp;
}ll solve(ll x)
{int pos=0;while(x){a[pos++]=x%10;x/=10;}return dfs(pos-1,0,true,true);
}
int main()
{memset(dp,-1,sizeof(dp));int T;cin>>T;for(int i=1;i<=T;i++){ll n,m;scanf("%lld%lld",&n,&m);if(n==0)printf("Case %d: %lld\n",i,solve(m)-solve(n-1)+1);//0是一个特殊情况elseprintf("Case %d: %lld\n",i,solve(m)-solve(n-1));}return 0;
}