洛谷 UVA1640 统计问题 The Counting Problem
和P2602一样
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int ans = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
ans = ans * 10 + ch - '0';
ch = getchar();
}
return ans * f;
}
const int N = 35;
int a[N];
int ans[10];
int f[N][N][10];
int dfs(int pos,bool limit, bool lead,int digit,int sum) {
if (!pos) {
return sum;
}
if (!limit && !lead && ~f[pos][sum][digit]) {
return f[pos][sum][digit];
}
int up = limit?a[pos]:9;
int ans= 0;
for (int i = 0; i<= up; i++) {
int tmp = sum;
tmp += (i==digit);
if (!i && lead && !digit)
tmp = 0;
ans += dfs(pos-1,limit && i== up, lead && i==0, digit,tmp);
}
if (!limit &&!lead)
f[pos][sum][digit] = ans;
return ans;
}
void work(int x,int op) {
int cnt= 0 ;
do {
a[++cnt] = x%10;
x/=10;
}while (x);
for (int i = 0; i< 10; i++) {
ans[i] += op * dfs(cnt,true,true,i,0);
}
}
void solve(int a,int b) {
for (int i = 0; i< 10; i++)ans[i] = 0;
work(b,1);
work(a-1,-1);
for (int i = 0;i<10; i++)
cout<<ans[i]<<" \n"[i==9];
}
int main() {
int x,y;
memset(f,-1,sizeof f);
while (cin>>x>>y) {
if (x==0 && y== 0)
return 0;
if (x>y)
swap(x,y);
solve(x,y);
}
return 0;
}

浙公网安备 33010602011771号