답안 #634156

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634156 2022-08-23T22:42:51 Z inksamurai Klavir (COCI17_klavir) C++17
144 / 160
1000 ms 36732 KB
#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;
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 3832 KB Output is correct
2 Correct 302 ms 33612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 402 ms 36704 KB Output is correct
2 Correct 282 ms 33668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 463 ms 36732 KB Output is correct
2 Correct 304 ms 33732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 34364 KB Time limit exceeded
2 Halted 0 ms 0 KB -