#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>;
const int _n=1000001;
int a[_n],pi[_n];
using pmi=pair<mint,mint>;
pmi e[_n];
signed main(){
_3PGDklf;
int n,m;
cin>>n>>m;
rep(i,m){
cin>>a[i];
a[i]-=1;
}
// e(0)*p.fi+p.se
e[0]={1,0};
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;
};
rep(i,m){
int u=a[i];
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);
// rep(v,n){
// if(v==u) continue;
// // if(i==1 and v==a[0]) continue;
// int nj=find_link(i,v);
// sun.fi+=e[nj].fi;
// sun.se+=e[nj].se+1;
// }
}else{
int nj=pi[i-1];
sun.fi=mint(n)*e[nj].fi-e[nj+1].fi;
sun.se=mint(n)*e[nj].se-e[nj+1].se-mint(1);
// print(e[nj+1].fi.x,e[nj+1].se.x);
// if(a[nj+1]!=u){
sun.fi=sun.fi+e[nj+1].fi;
sun.se=sun.se+e[nj+1].se+mint(1);
// }
nj=find_link(i,u);
sun.fi-=e[nj].fi;
sun.se-=(e[nj].se+mint(1));
// }
}
// print(i,sun.fi.x,sun.se.x);
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){
int nj=find_link(i,u);
pi[i]=nj;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15992 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15992 KB |
Output is correct |
2 |
Correct |
8 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
8 ms |
15884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
16064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
17764 KB |
Output is correct |
2 |
Correct |
319 ms |
30676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
33688 KB |
Output is correct |
2 |
Correct |
311 ms |
30700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
33728 KB |
Output is correct |
2 |
Correct |
280 ms |
30600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
33760 KB |
Output is correct |
2 |
Correct |
288 ms |
30668 KB |
Output is correct |