This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub beet-aizu/library
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0422 #include<bits/stdc++.h> using namespace std; #define call_from_test #include "../../tools/drop.cpp" #include "../../mod/rint.cpp" #undef call_from_test signed main(){ int n,k,m; cin>>n>>k>>m; using R = Rint<int>; R::set_mod(m); if(n>k*2) drop(R(k).pow(n)); int sz=1<<(k*2-1); vector< vector<R> > dp(k,vector<R>(sz,0)); for(int i=0;i<k;i++) dp[i][0]=1; R ans=R(k).pow(n); for(int bit=0;bit<sz;bit++){ for(int i=0;i<k;i++){ if(dp[i][bit]==0) continue; if(__builtin_popcount(bit)+1==n){ ans-=dp[i][bit]; } for(int j=0;j<k;j++){ int d=(i-j)+(k-1); if((bit>>d)&1) continue; dp[j][bit|(1<<d)]+=dp[i][bit]; } } } cout<<ans<<endl; return 0; }
#line 1 "test/aoj/0422.test.cpp" // verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0422 #include<bits/stdc++.h> using namespace std; #define call_from_test #line 1 "tools/drop.cpp" #line 3 "tools/drop.cpp" using namespace std; #endif //BEGIN CUT HERE template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #line 1 "mod/rint.cpp" #line 3 "mod/rint.cpp" using namespace std; #endif //BEGIN CUT HERE template<typename T> struct Rint{ inline static T mod; static void set_mod(T nmod){mod=nmod;} T v; Rint():v(0){} Rint(signed v):v(v){} Rint(long long t){v=t%mod;if(v<0) v+=mod;} Rint pow(long long k){ Rint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Rint add_identity(){return Rint(0);} static Rint mul_identity(){return Rint(1);} Rint inv(){return pow(mod-2);} Rint& operator+=(Rint a){v+=a.v;if(v>=mod)v-=mod;return *this;} Rint& operator-=(Rint a){v+=mod-a.v;if(v>=mod)v-=mod;return *this;} Rint& operator*=(Rint a){v=1LL*v*a.v%mod;return *this;} Rint& operator/=(Rint a){return (*this)*=a.inv();} Rint operator+(Rint a) const{return Rint(v)+=a;} Rint operator-(Rint a) const{return Rint(v)-=a;} Rint operator*(Rint a) const{return Rint(v)*=a;} Rint operator/(Rint a) const{return Rint(v)/=a;} Rint operator-() const{return v?Rint(mod-v):Rint(v);} bool operator==(const Rint a)const{return v==a.v;} bool operator!=(const Rint a)const{return v!=a.v;} bool operator <(const Rint a)const{return v <a.v;} }; template<typename T> ostream& operator<<(ostream &os,Rint<T> m){os<<m.v;return os;} //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #line 9 "test/aoj/0422.test.cpp" #undef call_from_test signed main(){ int n,k,m; cin>>n>>k>>m; using R = Rint<int>; R::set_mod(m); if(n>k*2) drop(R(k).pow(n)); int sz=1<<(k*2-1); vector< vector<R> > dp(k,vector<R>(sz,0)); for(int i=0;i<k;i++) dp[i][0]=1; R ans=R(k).pow(n); for(int bit=0;bit<sz;bit++){ for(int i=0;i<k;i++){ if(dp[i][bit]==0) continue; if(__builtin_popcount(bit)+1==n){ ans-=dp[i][bit]; } for(int j=0;j<k;j++){ int d=(i-j)+(k-1); if((bit>>d)&1) continue; dp[j][bit|(1<<d)]+=dp[i][bit]; } } } cout<<ans<<endl; return 0; }