您的位置:首页 > 其它

【Light oj 1140 - How Many Zeroes?】 + 数位 dp

2017-04-08 18:15 351 查看
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

数位dp

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
int s[20];
LL dp[30][30];
LL js(int n,int a,int b,int c){
if(n < 0) return c ? 1 : a;
if(!c && !b && dp
[a] != -1) return dp
[a]; // 剪枝,当不是临界值或者第一追溯时,不必再计算
int z = b ? s
: 9; // 临界
LL ans = 0;
for(int i = 0; i <= z; i++){
if(c && !i) ans += js(n - 1,0,b && i == z,1); // 第一次追溯
else if(!i) ans += js(n - 1,a + 1,b && i == z,0); // 统计数再次出现
else  ans += js(n - 1,a,b && i == z,0);
}
if(!c && !b) dp
[a] = ans; // 剪枝
return ans;
}
LL sl(LL a){
if(a < 0) return 0;
if(a == 0) return 1;
memset(dp,-1,sizeof(dp));
int m  = 0;
while(a){
s[m++] = a % 10;
a /= 10;
}
return js(m - 1,0,1,1);
}
int main()
{
int T,nl = 0;
LL n,m;
scanf("%d",&T);
while(T--){
scanf("%lld %lld",&n,&m);
printf("Case %d: %lld\n",++nl,sl(m) - sl(n - 1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp