Hydro OJ 1008 序列划分
题意
给一个长度为
给一个整数
输出方案总数对
部分分
我们先来考虑
为什么
令
那么如果当前段能被整除,
那么必须从
满分做法
你发现可以转移时进行拆分
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=5e5+5,mod=1e9+7;
int tot[2];//表示
int tong[maxn][2],g[maxn][2],dig[maxn],a[maxn];//表示以a[i]作为结尾的方案数
int d,d1,d2=1,t,k,n;
char ch[maxn];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>(ch+1);
cin>>d;
n=strlen(ch+1);
while(!(d%5)) {
d/=5;
d2*=5;
}
while(!(d%2)) {
d/=2;
d2<<=1;
}
d1=d;//互质类与剩余类
for(int i=1; i<=n; ++i) dig[i]=ch[i]-'0';
for(int i=1; i<=n; ++i) a[i]=(a[i-1]*10%d1+dig[i])%d1;//表示互质的能被整除
int f=1;
for(int i=n; i>0; --i) {
a[i]=a[i]*f%d1;
f=f*10%d1;
}
g[0][1]=1;
tot[1]=1;//表示tot的总和
tong[0][1]=1;//表示a[0] 表示每个值域的答案
for(int i=1; i<=n; ++i) {
t=0;
f=1;k=1;
for(int j=i; j>0&&j>=i-20; --j) {
t=(t+dig[j]*k%d2)%d2;//表示能否满足第二个条件
k=((k<<1)%d2+(k<<3)%d2)%d2;
}
int x=0,y=0;
if(t==0) {
k=1;
x=tong[a[i]][0],y=tong[a[i]][1];
for(int j=i; j>0&&j>=i-20; --j) {
t=(t+dig[j]*k%d2)%d2;//表示能否满足第二个条件
if((t!=0)&&a[j-1]==a[i]){
x=((x-g[j-1][0])%mod+mod)%mod,y=((y-g[j-1][1])%mod+mod)%mod;
}
k=((k<<1)+(k<<3))%d2;
}
}
else{
t=0;k=1;
for(int j=i; j>0&&j>=i-20; --j) {
t=(t+dig[j]*k%d2)%d2;//表示能否满足第二个条件
if(t==0&&a[j-1]==a[i]){
x=(x+g[j-1][0])%mod,y=(y+g[j-1][1])%mod;
}
k=((k<<1)+(k<<3))%d2;
}
}
g[i][0]=((tot[1]-y)%mod+mod)%mod;g[i][1]=(x+y)%mod;//就 是 总 的 减 去 可 以 被 整 除 的 情 况
tong[a[i]][0]=(tong[a[i]][0]+g[i][0])%mod;tong[a[i]][1]=(tong[a[i]][1]+g[i][1])%mod;
tot[1]=(tot[1]+g[i][1])%mod,tot[0]=(tot[0]+g[i][0])%mod;
}
printf("%lld",((g[n][1]+g[n][0])%mod));
return 0;
}