BZOJ1833: [ZJOI2010]count 数字计数
1833: [ZJOI2010]count 数字计数
Time Limit: 3 Sec Memory Limit: 64 MB
Submit: 4225 Solved: 1858
[Submit][Status][Discuss]
Description
给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。
Input
输入文件中仅包含一行两个整数a、b,含义如上所述。
Output
输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。
Sample Input
1 99
Sample Output
9 20 20 20 20 20 20 20 20 20
HINT
30%的数据中,a<=b<=10^6;
100%的数据中,a<=b<=10^12。
Source
Day1
题解
dp[i][j][k]表示i位,最高位j,k的个数
套路题
注意格式,行末不要有多余空格。。为此PE
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline long long abs(long long x){return x < 0 ? -x : x;}
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
//dp[i][j][k]表示共i位,最高位是j,k出现了多少次
long long dp[15][10][10], ans[10], pow[20], num[20];
long long solve(long long x, long long b)
{
long long M = 0, tmp = x, re = 0;
while(tmp) num[++ M] = tmp % 10, tmp /= 10;
/*for(long long i = 0;i <= 9;++ i)
re += dp[M - 1][i][b];
if(!b) re -= dp[M - 1][0][b];*/
for(long long i = 1;i < M;++ i)
for(long long j = 1;j <= 9;++ j)
re += dp[i][j][b];
for(long long i = 1;i < num[M];++ i)
re += dp[M][i][b];
tmp = x;
if(num[M] == b) re += x - pow[M - 1] * num[M];
tmp -= pow[M - 1] * num[M];
for(long long i = M - 1;i >= 1;-- i)
{
if(num[i] == b) re += tmp - pow[i - 1] * num[i];
tmp -= pow[i - 1] * num[i];
for(long long j = 0;j < num[i];++ j)
re += dp[i][j][b];
}
return re;
}
int main()
{
pow[0] = 1;
for(long long i = 1;i <= 13;++ i) pow[i] = pow[i - 1] * 10;
for(long long i = 1;i <= 12;++ i)
for(long long j = 0;j <= 9;++ j)
for(long long k = 0;k <= 9;++ k)
{
for(long long p = 0;p <= 9;++ p)
dp[i][j][k] += dp[i - 1][p][k];
if(j == k) dp[i][j][k] += pow[i - 1];
}
long long a, b;read(a), read(b);
if(a > b) swap(a, b);
printf("%lld", solve(b + 1, 0) - solve(a, 0));
for(long long i = 1;i <= 9;++ i)
printf(" %lld", solve(b + 1, i) - solve(a, i));
return 0;
}