This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3PGDklf ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
//snuke's mod int
template <ll mod>
struct modint{
ll x; // typedef long long ll;
modint(ll x=0):x((x%mod+mod)%mod){}
modint operator-()const{return modint(-x);}
modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
};
using mint=modint<1000000007>;
signed main(){
_3PGDklf;
int n,m;
cin>>n>>m;
vi a(m);
rep(i,m){
cin>>a[i];
a[i]-=1;
}
using pmi=pair<mint,mint>;
vec(pmi) e(m+1);
// e(0)*p.fi+p.se
e[0]={1,0};
vi pi(m);
auto find_link=[&](int i,int ch)->int{
if(!i){
return ch==a[0];
}else{
int j=pi[i-1];
while(j and a[j]!=ch){
j=pi[j-1];
}
j+=a[j]==ch;
return j;
}
};
rep(i,m){
int u=a[i];
int cnt=0;
pmi ad={0,0};
rep(v,n){
if(v==u) continue;
// suppose i had matching with length i and pushed character v
// find length of the lcsuff
int nj=find_link(i,v);
assert(nj<=i);
if(nj==i){
cnt+=1;
}else{
ad.fi+=e[nj].fi;
ad.se+=e[nj].se+1;
}
}
pmi now;
now.fi=mint(n)*e[i].fi-ad.fi-mint(cnt)*e[i].fi;
now.se=mint(n)*e[i].se-ad.se-mint(1)-mint(cnt)*e[i].se-mint(cnt);
e[i+1]=now;
// now.fi*e[0]+now.se=0
// now.fi*e[0]=-now.se
mint x=-now.se*now.fi.inv();
print(x.x);
if(i>0){
int nj=find_link(i,u);
pi[i]=nj;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |