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;
}
vi pi(m+1);
auto find_link=[&](int i,int ch)->int{
if(!i){
return ch==a[0];
}
int j=pi[i-1];
while(j and a[j]!=ch){
j=pi[j-1];
}
j+=a[j]==ch;
return j;
};
using pmi=pair<mint,mint>;
vec(pmi) e(m+1);
// e(0)*p.fi+p.se
e[0]={1,0};
rep(i,m){
int u=a[i];
int link=find_link(i,u);
pmi sun={0,0};
if(!i){
sun.fi=e[0].fi*mint(n-1);
sun.se=e[0].se*mint(n-1)+mint(n-1);
}else{
int nj=pi[i-1];
sun.fi=mint(n)*e[nj].fi-e[link].fi;
sun.se=mint(n)*e[nj].se-(e[link].se+mint(1));
}
pmi now;
now.fi=mint(n)*e[i].fi-sun.fi;
now.se=mint(n)*e[i].se-sun.se-mint(1);
e[i+1]=now;
mint x=-now.se*now.fi.inv();
cout<<x.x<<" ";
if(i>0){
pi[i]=link;
}
}
}
# | 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... |