社区讨论
样例没过,但AC了,板子数位DP求破案
UVA1640统计问题 The Counting Problem参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lo3dfu8l
- 此快照首次捕获于
- 2023/10/24 04:49 2 年前
- 此快照最后确认于
- 2023/10/24 04:49 2 年前
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 15;
int num[N];
LL l, r, f[N];
LL dfs(int pos, bool lim, bool zero, int dig){
if(!pos) return 0;
if(!lim && !zero && f[pos]) return f[pos];
int maxn = lim ? num[pos] : 9, minn = zero ? 1 : 0;
LL res = 0;
for(int i = minn; i <= maxn; i++){
if(i == dig && (i != num[pos] || !lim)) res += pow(10, pos - 1);
else if(i == dig){
LL t = 0;
for(int j = pos - 1; j; j--)
t *= 10, t += num[j];
res += t + 1;
}
res += dfs(pos - 1, lim && i == num[pos], false, dig);
}
if(zero) res += dfs(pos - 1, false, zero, dig);
if(!lim) f[pos] = res;
return res;
}
inline LL work(LL x, int dig){
memset(f, 0, sizeof f);
int len = 0;
while(x){
num[++ len] = x % 10;
x /= 10;
}
return dfs(len, true, true, dig);
}
int main(){
while(323232){
cin >> l >> r;
if(!l && !r) break;
if(l > r) swap(l, r);
for(int i = 0; i < 9; i++)
cout << work(r, i) - work(l - 1, i) << ' ';
cout << work(r, 9) - work(l - 1, 9) << endl;
}
return 0;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...