298人阅读 评论(0)

# 考虑暴力

ri=l9j=1f[i][j]$\sum_{i=l}^r\sum_{j=1}^9f[i][j]$

ans[a[i]]=1+9j=1ans[j]$ans[a[i]]=1+\sum_{j=1}^9ans[j]$

9j=1ans[j]$\sum_{j=1}^9ans[j]$

# 考虑优化

B[l1]B[l2]B[1]A[1]A[r]$B[l-1]*B[l-2]*…*B[1]*A[1]*…*A[r]$

#include<cstdio>
#include<algorithm>
#include<cstring>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn=100000+10,mo=1000000007;
int a[9][10][10],b[9][10][10],sum[maxn][10][10],num[maxn][10][10],o[10][10],c[10],d[10];
char s[maxn];
int i,j,k,l,r,t,n,m,ans;
bool czy;
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*10+ch-'0';
ch=getchar();
}
return x*f;
}
int qsm(int x,int y){
if (!y) return 1;
int t=qsm(x,y/2);
t=(ll)t*t%mo;
if (y%2) t=(ll)t*x%mo;
return t;
}
void getb(int x){
int i,j,k,t;
fo(i,0,9)
b[x][i][i]=1;
fo(i,0,9){
fo(j,i,9)
if (a[x][j][i]){
fo(k,i,9){
swap(a[x][j][k],a[x][i][k]);
swap(b[x][j][k],b[x][i][k]);
}
break;
}
fo(j,i+1,9){
t=(ll)a[x][j][i]*qsm(a[x][i][i],mo-2)%mo;
fo(k,i,9){
(a[x][j][k]-=(ll)a[x][i][k]*t%mo)%=mo;
(b[x][j][k]-=(ll)b[x][i][k]*t%mo)%=mo;
}
}
}
fd(i,9,0){
fo(j,i+1,9)
if (a[x][i][j]){
fo(k,i,9){
(a[x][i][k]-=a[x][j][k])%=mo;
(b[x][i][k]-=b[x][j][k])%=mo;
}
}
}
}
int main(){
freopen("sub.in","r",stdin);freopen("sub.out","w",stdout);
scanf("%s",s+1);
n=strlen(s+1);
fo(i,0,8){
fo(j,0,9) a[i][j][j]=1;
fo(j,0,9) a[i][j][i]=1;
getb(i);
fo(j,0,9)
fo(k,0,9)
a[i][j][k]=0;
fo(j,0,9) a[i][j][j]=1;
fo(j,0,9) a[i][j][i]=1;
}
/*fo(i,0,9)
fo(j,0,9)
o[i][j]=0;
fo(k,0,9)
fo(i,0,9)
fo(j,0,9)
(o[i][j]+=(ll)a[1][i][k]*b[1][k][j]%mo)%=mo;
czy=1;
fo(i,0,9)
fo(j,0,9)
if (o[i][j]!=(i==j?1:0)){
czy=0;
break;
}
printf("%d\n",czy);*/
fo(i,0,9) sum[0][i][i]=num[0][i][i]=1;
fo(l,1,n){
t=s[l]-'a';
fo(i,0,9)
fo(j,0,9)
o[i][j]=0;
fo(k,0,9)
fo(i,0,9)
fo(j,0,9)
(o[i][j]+=(ll)sum[l-1][i][k]*a[t][k][j]%mo)%=mo;
fo(i,0,9)
fo(j,0,9)
sum[l][i][j]=o[i][j];
fo(i,0,9)
fo(j,0,9)
o[i][j]=0;
fo(k,0,9)
fo(i,0,9)
fo(j,0,9)
(o[i][j]+=(ll)b[t][i][k]*num[l-1][k][j]%mo)%=mo;
fo(i,0,9)
fo(j,0,9)
num[l][i][j]=o[i][j];
}
while (m--){
fo(i,0,9) c[i]=d[i]=0;
c[9]=1;
fo(k,0,9)
fo(j,0,9)
(d[j]+=(ll)c[k]*num[l-1][k][j]%mo)%=mo;
fo(i,0,9) c[i]=d[i],d[i]=0;
fo(k,0,9)
fo(j,0,9)
(d[j]+=(ll)c[k]*sum[r][k][j]%mo)%=mo;
ans=0;
fo(i,0,8) (ans+=d[i])%=mo;
(ans+=mo)%=mo;
printf("%d\n",ans);
}
}

个人资料
等级：
访问量： 39万+
积分： 1万+
排名： 1456
最新评论
文章分类