http://codeforces.com/problemset/problem/1073/E
题意 给定K,L,R,求L~R之间最多不包含超过K个数码的数的和。
显然这是一道数位dp,在做的过程中会发现为了统计数码是否出现过这个状态需要用到状态压缩
因为不同位置出现的数贡献不同,除了传统的dp数组之外还需要记录一个tot来统计这个位置之后出现数字的个数方便后面计算答案。
仔细对比了一下为什么我要开五维数组而网上的题解只需要开二维的数组,发现一是因为网上的题解不对前导0和limit的情况进行记忆化搜索,仔细想了一下这两种情况确实分支的情况比较小,为了他开两倍的空间的确不那么值得。
二是因为dp除了记录位置和是否出现过这个state之外,每个位置是什么数事实上是不需要记录的,对于一个位置和一个状态事实上后续就有固定的答案,不需要额外开一个数字来记录,对于后效答案的更新可以放在进入记忆话搜索之前,因而数字这位数字也可以省略掉。
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
inline int read(){int now=0;register char c=getchar();for(;!isdigit(c);c=getchar());
for(;isdigit(c);now=now*10+c-'0',c=getchar());return now;}
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
const double eps = 1e-9;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
const LL mod = 998244353 ;
LL L,R;
int K;
LL ten[maxn];
int str[maxn];
LL dp[20][10][1100][2][2];
LL tot[20][10][1100][2][2];
int cnt;
LL dfs(int pos,int num,int use,int k,int limit,bool zero){
if(pos == 0){
tot[pos][num][use][limit][zero] = 1;
return num;
}
if(~dp[pos][num][use][limit][zero]) return dp[pos][num][use][limit][zero];
LL ed = limit?str[pos - 1]:9;
LL sum = 0;
for(int i = 0 ; i <= ed; i ++){
if(!(use & (1 << i)) && (zero || i)){
if(k + 1 > K) continue;
LL x = dfs(pos - 1,i,use | (1 << i),k + 1,limit && i == str[pos - 1],zero || i);
sum = (sum + x) % mod;
tot[pos][num][use][limit][zero] += tot[pos - 1][i][use | (1 << i)][limit && i == (str[pos - 1])][zero || i];
}else{
LL x = dfs(pos - 1,i,use,k,limit && i == str[pos - 1],zero || i);
sum = (sum + x) % mod;
tot[pos][num][use][limit][zero] += tot[pos - 1][i][use][limit && i == (str[pos - 1])][zero || i];
}
}
tot[pos][num][use][limit][zero] %= mod;
if(zero) sum = (sum + tot[pos][num][use][limit][zero] * num % mod * ten[pos]) % mod;
dp[pos][num][use][limit][zero] = sum;
return sum;
}
LL solve(LL x){
Mem(dp,-1); Mem(tot,0);
if(x <= 0) return 0;
cnt = 0;
while(x){
str[cnt++] = x % 10;
x /= 10;
}
return dfs(cnt,0,0,0,1,0);
}
int main()
{
ten[0] = 1;
for(int i = 1; i <= 18; i ++) ten[i] = (ten[i - 1] * 10) % mod;
scanf("%lld%lld%d",&L,&R,&K);
LL ans1 = solve(R),ans2 = solve(L - 1);
//cout << ans1 << " " << ans2 << endl;
Prl((ans1 - ans2 + mod) % mod);
#ifdef VSCode
system("pause");
#endif
return 0;
}
来源:oschina
链接:https://my.oschina.net/u/4299463/blog/3751875