1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ #define FS 100000 #define tc() (FA==FB&&(FB=(FA=FI)+fread(FI,1,FS,stdin),FA==FB)?EOF:*FA++) #define pc(c) (FC==FE&&(clear(),0),*FC++=c) int OT;char oc,FI[FS],FO[FS],OS[FS],*FA=FI,*FB=FI,*FC=FO,*FE=FO+FS; I void clear() {fwrite(FO,1,FC-FO,stdout),FC=FO;} Tp I void read(Ty& x) {x=0;RI f=1;W(!isdigit(oc=tc())) f=oc^'-'?1:-1;W(x=(x<<3)+(x<<1)+(oc&15),isdigit(oc=tc()));x*=f;} Ts I void read(Ty& x,Ar&... y) {read(x),read(y...);} Tp I void write(Ty x) {x<0&&(pc('-'),x=-x);W(OS[++OT]=x%10+48,x/=10);W(OT) pc(OS[OT--]);} Tp I void writeln(Ty x) {x<0&&(pc('-'),x=-x);W(OS[++OT]=x%10+48,x/=10);W(OT) pc(OS[OT--]);pc('\n');} }using namespace FastIO; Cn int N=5e3+10; int T,p,n,Ans,fac[N],ifac[N],g[N],f[N],s[N]; I int QP(RI a,RI b){if(b<0) return 0;RI s=1;W(b) b&1&&(s=1LL*s*a%p),a=1LL*a*a%p,b>>=1;return s;} I int C(CI n,CI m){return n<m?0:1LL*fac[n]*ifac[n-m]%p*ifac[m]%p;} int main(){ freopen("forest.in","r",stdin),freopen("forest.out","w",stdout); RI i,j,o;read(T,p);for(fac[0]=1,i=1;i<N;i++) fac[i]=1LL*fac[i-1]*i%p;for(ifac[N-1]=QP(fac[N-1],p-2),i=N-2;~i;i--) ifac[i]=1LL*ifac[i+1]*(i+1)%p; for(f[1]=1,i=2;i<N;i++) f[i]=QP(i,i-2)%p; for(g[0]=1,i=1;i<N;i++) for(j=1;j<=i;j++) g[i]+=1LL*C(i-1,j-1)*g[i-j]%p*f[j]%p,g[i]%=p; for(i=2;i<N;i++) for(o=1,j=i-1;j;j--) s[i]+=1LL*C(i-2,j-1)*o%p*j%p*j%p,s[i]%=p,o=1LL*o*(i-1)%p;W(T--){ for(read(n),Ans=0,i=1;i<=n;i++) Ans+=1LL*C(n-1,i-1)*g[n-i]%p*s[i]%p,Ans%=p; writeln(1LL*Ans*n%p); }return clear(),0; }
|