# 【LOJ #6074】「2017 山东一轮集训 Day6」子序列（矩阵乘法）

$f[i][9]$则表示之前一个都没选的方案

$A_i=\left[\begin{matrix}1&0 \cdots1 &0\cdots0 \\0 & 1 \cdots1 &0 \cdots 0 \\ \vdots &\ddots\ 1 &0\cdots 0\\0 &0\cdots1 &0\cdots 0\\0 & 0 \cdots 1 &0 \cdots 1 \end{matrix}\right]$

$\left[\begin{matrix} 0&0&\cdots &1\end{matrix}\right]*A_l*A_{l+1}\cdots*A_{r}*\left[\begin{matrix} 1\\ 1\\ \vdots \\1\end{matrix}\right]$

$B=A^{-1}$

$\left[\begin{matrix}1&0 \cdots -1 &0\cdots0 \\0 & 1 \cdots -1 &0 \cdots 0 \\ \vdots &\ddots\ 1 &0\cdots 0\\0 &0\cdots-1 &0\cdots 0\\0 & 0 \cdots -1 &0 \cdots 1 \end{matrix}\right]$
$A*B=B*A=I$

$\left[\begin{matrix} 0&0&\cdots &1\end{matrix}\right]*B_{l-1}*B_{l-2}\cdots*B_1*A_l*A_{l+1}\cdots*A_{r}*\left[\begin{matrix} 1\\ 1\\ \vdots \\1\end{matrix}\right]$

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstr(int *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch))s[++top]=ch-'a',ch=gc();
return top;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=100005;
int a[N][10],b[N][10],ta[10][10],sum[10],tb[10][10],tag[10],n;
int s[N];
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=readstr(s);
b[0][9]=1;
for(int i=0;i<10;i++)tb[i][i]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<10;j++){
int k=tag[j];tag[j]=tb[s[i]][j];
tb[s[i]][j]=dec(mul(tb[s[i]][j],2),k);
}
for(int j=0;j<10;j++)b[i][j]=dec(tb[9][j],tag[j]);
}
for(int i=0;i<10;i++)sum[i]=a[i][i]=1,ta[i][i]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<10;j++){
int tp=sum[j];Dec(sum[j],ta[j][s[i]]);
ta[j][s[i]]=tp,Add(sum[j],ta[j][s[i]]);
}
for(int j=0;j<10;j++)a[i][j]=sum[j];
}
int q=read();
while(q--){
int l=read(),r=read();
int res=0;
for(int i=0;i<10;i++)Add(res,mul(b[l-1][i],a[r][i]));
cout<<dec(res,1)<<'\n';
}
}


©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客