【P2602 [ZJOI2010]数字计数】题解
题目链接
题目
原题来自:ZJOI 2010
给定两个正整数 \(a\) 和 \(b\),求在 [\(a,b\)] 中的所有整数中,每个数码 (\(digit\)) 各出现了多少次。
思路
首先在数位dp中,对于当前枚举的数,乘上后面的方案数。
那么后面的数如何多次计算呢?
我们发现这些数具有传递性,于是我们每次可以把后面的数传到前面来。
然后在每次记忆化搜索返回时,顺便加上后面的数即可。
总结
这道题总得来说还是不错的。
然而难点在于,当每次记忆化搜索返回时,后面的缺少统计了,于是我就尝试把他记起来。
然后又发现,在更后面的数又缺少统计,于是我发现这些统计的数由于记忆化搜索每次一样,所以具有传递性和普遍性,然后可以传递过来统计。
这也是数位dp的精妙之一。
Code
// Problem: P2602 [ZJOI2010]数字计数
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2602
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k;
int f[30][2][2], s[30][15][2][15], a[30], ans[15];
int x, y;
int dfs(int k, int p, int o)
{
if(k==0) return 1;
if(f[k][p][o]!=-1)
{
for(int i=0; i<=9; ++i) ans[i]+=s[k][p][o][i];
return f[k][p][o];
}
int i, j, q, z;
for(i=j=0; i<=9; ++i)
{
if(!p && i>a[k]) break;
q=dfs(k-1, p||(i
相关