Submission #634164

#TimeUsernameProblemLanguageResultExecution timeMemory
634164inksamuraiKlavir (COCI17_klavir)C++17
96 / 160
1091 ms65536 KiB
#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]; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // https://codeforces.com/blog/entry/62393 // cf684 b1 struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int,int> x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; 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}; gp_hash_table<ll,int,custom_hash> es; auto find_link=[&](int i,int ch)->int{ if(!i){ return ch==a[0]; } int j=pi[i-1],pj=pi[i-1]; if(es[j*101+ch]>0) return es[j*101+ch]-1; while(j and a[j]!=ch){ j=pi[j-1]; } j+=a[j]==ch; es[pj*101+ch]=j+1; return j; }; pmi now={0,0},ad={0,0}; rep(i,m){ int u=a[i]; 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); ad.fi+=e[nj].fi; ad.se+=e[nj].se+1; } now.fi=mint(n)*e[i].fi-ad.fi; now.se=mint(n)*e[i].se-ad.se-mint(1); e[i+1]=now; // now.fi*e[0]+now.se=0 // now.fi*e[0]=-now.se mint x=-now.se*now.fi.inv(); cout<<x.x<<" "; if(i>0){ int nj=find_link(i,u); pi[i]=nj; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...