题意:求本质不同的回文串(大整数)的数字和
由回文树的性质可知贡献只在首次进入某个新节点时产生
那么只需由pos和len算出距离把左边右边删掉再算好base重复\(O(n)\)次即可
位移那段写的略微凌乱..
#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define rrep(i,j,k) for(int i=j;i>=k;i--)
#define println(a) printf("%lld\n",(ll)(a))
#define printbk(a) printf("%lld ",(ll)(a))
typedef long long ll;
using namespace std;
const int MAXN = 2e6+11;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const ll ooo= 0x3f3f3f3f;
const int MOD = 1000000007;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
char str[MAXN];
ll _10[MAXN],inv10[MAXN];
ll sum[MAXN],rsum[MAXN];
ll ANS;
int llen;
typedef long long LL;
LL pow_mod(LL a, LL b, LL p){//a的b次方求余p
LL ret = 1;
while(b){
if(b & 1) ret = (ret * a) % p;
a = (a * a) % p;
b >>= 1;
}
return ret;
}
LL fermat(LL a, LL p){//费马求a关于b的逆元
return pow_mod(a, p-2, p);
}
struct PT{
char s[MAXN];
int last,cur,tot;
int son[MAXN][10];
int fail[MAXN],len[MAXN];
void init(){
s[0]=-1;
last=cur=0;
tot=1;
rep(i,0,9) son[0][i]=son[1][i]=0;
len[0]=0,len[1]=-1;
fail[0]=1;fail[1]=0;
}
int node(int l){
++tot;
rep(i,0,9) son[tot][i]=0;
fail[tot]=0;
len[tot]=l;
return tot;
}
int getfail(int x){
while(s[cur-len[x]-1]^s[cur]) x=fail[x];
return x;
}
void add(int pos){
s[++cur]=str[pos];
int t=getfail(last);
int c=str[pos]-‘0‘;
if(son[t][c]==0){
int o=node(len[t]+2);
fail[o]=son[getfail(fail[t])][c];
son[t][c]=o;
ll lo=((sum[llen]-rsum[pos-(len[t]+2)+1])%MOD+MOD)%MOD;
ll hi=rsum[pos+1];
ll all=lo+hi; if(all>=MOD) all%=MOD;
ll t=((sum[llen]-all)%MOD+MOD)%MOD;
if(t>=MOD) t%=MOD;
if(llen-pos+1-1>0)ANS=ANS+t*inv10[llen-pos+1-1]%MOD;
else ANS=ANS+t;
if(ANS>=MOD) ANS%=MOD;
}
last=son[t][c];
}
}pt;
int main(){
_10[0]=1;
rep(i,1,MAXN-1){
_10[i]=_10[i-1]*10;
if(_10[i]>=MOD) _10[i]%=MOD;
inv10[i]=fermat(_10[i],MOD);
}
while(~scanf("%s",str+1)){
memset(sum,0,sizeof sum);
memset(rsum,0,sizeof rsum);
ANS=0;
sum[0]=0;
pt.init();
llen=strlen(str+1);
rep(i,1,llen){
sum[i]=sum[i-1]*10+(int)(str[i]-‘0‘);
if(sum[i]>=MOD) sum[i]%=MOD;
}
rsum[llen+1]=0;
rrep(i,llen,1){
rsum[i]=rsum[i+1]+(int)(str[i]-‘0‘)*_10[llen-i]%MOD;
if(rsum[i]>=MOD) rsum[i]%=MOD;
}
ANS=0;
rep(i,1,llen) pt.add(i);
println(ANS%MOD);
}
return 0;
}
原文地址:https://www.cnblogs.com/caturra/p/9576499.html
时间: 2024-11-09 10:03:19