题解:P11435 [Code+#8 决赛] 报数Ⅱ
怎么这么卡常。
前两个限制是数位 DP 容易做到的。
对于第一个,我们只需要会 KMP,就可以快速找到当前这位对应的
困难的是我们需要在维护前缀是否合法时还要考虑后缀。
我们肯定无法在 dp 过程中既维护前缀又维护后缀,考虑提前钦定整个数模
然后就是卡常了,我们发现在转移过程中,我们会有很多的取模,然后我们就炸了,我们考虑预处理出每位可能的情况,以减少取模次数。
::::success[code]
#include<bits/stdc++.h>
// #define debug
#define ll long long
#define ull unsigned long long
#define int long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define dep(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
const int N=2e5+15,mod=998244353;
bool Mst;
int l,r,a,b,c,s[20];signed fail[20],n,now;
int mx[20],pw[20];signed tot;
int pb[301][11],pc[301][20][11];
int f[20][2][2][11][300][300];
bitset<300> g[20][2][2][11][300];
int f_[20][2][2][11][300][300];
bitset<300> g_[20][2][2][11][300];// 直接清空会比较慢,偷了点懒,直接写粘了一遍,空间是开的下的
void get(int x,int a[],signed&tot){
tot=0;
while(x) a[++tot]=x%10,x/=10;
}
void init(){
get(a,s,n);
reverse(s+1,s+1+n);
signed l=0;
rep(i,2,n){
while(l&&s[i]!=s[l+1]) l=fail[l];
if(s[i]==s[l+1]) l++;
fail[i]=l;
}
}
inline signed get(int x,int l){
while(l&&x!=s[l+1]) l=fail[l];
if(x==s[l+1]) l++;
return l;
}
int dfs1(int u,bool zr,bool fl,int x,int y,int z){
if(!u) return x!=n&&y&&!z;
if(!zr&&(x==n||!y||!z)) return 0;
if(g[u][zr][fl][x][y][z]) return f[u][zr][fl][x][y][z];
g[u][zr][fl][x][y][z]=1;
rep(i,0,fl?mx[u]:9) f[u][zr][fl][x][y][z]+=dfs1(u-1,zr&&i==0,fl&&i==mx[u],get(i,x),pb[y][i],pc[z][u][i]);
return f[u][zr][fl][x][y][z];
}
int solve1(int n){
int cnt=0;
get(n,mx,tot);
rep(i,1,c-1) cnt+=dfs1(tot,1,1,0,0,i);
return cnt;
}
int dfs2(int u,bool zr,bool fl,int x,int y,int z){
if(!u) return x!=n&&y&&!z;
if(!zr&&(x==n||!y||!z)) return 0;
if(g_[u][zr][fl][x][y][z]) return f_[u][zr][fl][x][y][z];
g_[u][zr][fl][x][y][z]=1;
rep(i,0,fl?mx[u]:9) f_[u][zr][fl][x][y][z]+=dfs2(u-1,zr&&i==0,fl&&i==mx[u],get(i,x),pb[y][i],pc[z][u][i]);
return f_[u][zr][fl][x][y][z];
}
int solve2(int n){
int cnt=0;
get(n,mx,tot);
rep(i,1,c-1) cnt+=dfs2(tot,1,1,0,0,i);
return cnt;
}
bool Med;
signed main(){
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>l>>r>>a>>b>>c;
pw[1]=1;
rep(i,2,19) pw[i]=pw[i-1]*10;
rep(i,0,b) rep(j,0,9) pb[i][j]=(i*10+j)%b;
rep(i,0,c) rep(j,0,19) rep(k,0,9) pc[i][j][k]=(i-pw[j]*k%c+c)%c;
init();
cout<<solve1(r)-solve2(l-1)<<'\n';
#ifdef debug
cerr<<"Memory:"<<(&Mst-&Med)/1024.0/1024.0<<" MB\n";
cerr<<"Time:"<<clock()/1.0/CLOCKS_PER_SEC*1000.0<<" ms\n";
#endif
return 0;
}
::::