#include "message.h" #include "futbol.h" #include "bits/stdc++.h" using namespace std; using i64=long long; using u32=unsigned int; using u64=unsigned long long; using pii=pair<int,int>; const int S=65,N=S+5,U=2048; int mod; template<class T>void exgcd(T a,T b,T&x,T&y){ if(!b)return (void)(x=1,y=0); exgcd(b,a%b,y,x),y-=a/b*x; } template<class T>T Inv(T a,T p){ T x,y;exgcd(a,p,x,y); return (x%p+p)%p; } int fpow(int a,int t){ int r; for(r=1;t;a=(i64)a*a%mod,t>>=1)if(t&1)r=(i64)r*a%mod; return r; } struct num{ int x,y; num(){x=0,y=1;} num(int x,int y):x(x),y(y){} friend num operator+(num a,num b){ i64 p=(i64)a.x*b.y+(i64)a.y*b.x; i64 q=(i64)a.y*b.y; return num(p%mod,q%mod); } friend num operator-(num a,num b){ i64 p=(i64)a.x*b.y-(i64)a.y*b.x; i64 q=(i64)a.y*b.y; return num((p%mod+mod)%mod,q%mod); } friend num operator*(num a,num b){ i64 p=(i64)a.x*b.x; i64 q=(i64)a.y*b.y; return num(p%mod,q%mod); } int value(){return (i64)x*Inv(y,mod)%mod;} }; int n,m; struct fp{ int f[N],g[N],h[N],c[N][N]; fp(){ memset(f,0,sizeof(f)),memset(g,0,sizeof(g)); memset(h,0,sizeof(h)),memset(c,0,sizeof(c)); } void init(){ int x,y; f[0]=g[0]=1; for(y=1;y<=S;++y){ for(x=y;x>=0;--x){ f[x]=(i64)f[x]*(n+mod-y+1)%mod; if(x)f[x]=(f[x]+(i64)f[x-1]*(mod-S))%mod; g[x]=(i64)g[x]*y%mod; if(x)g[x]=(g[x]+(i64)g[x-1]*S)%mod; h[x]=(i64)h[x]*y%mod; if(x)h[x]=(h[x]+(i64)h[x-1]*S)%mod; h[x]=(h[x]+f[x])%mod; } } for(x=0;x<=S;++x)for(y=c[x][0]=1;y<=x;++y)c[x][y]=(c[x-1][y]+c[x-1][y-1])%mod; } void mov(int w){ int x,y,p; for(x=1;x<=S;++x)for(y=x-1,p=w;y>=0;--y,p=(i64)p*w%mod){ f[y]=(f[y]+(i64)f[x]*p%mod*c[x][y])%mod; g[y]=(g[y]+(i64)g[x]*p%mod*c[x][y])%mod; h[y]=(h[y]+(i64)h[x]*p%mod*c[x][y])%mod; } } pair<num,num> get(u32 w){ int x; u64 sf=0,sg=0,sh=0; u64 w3=(u64)w*w*w,w2=w*w,w1=w; for(x=S;x>=0;x-=3){ sf=(sf*w3+f[x]*w2+f[x-1]*w1+f[x-2])%mod; sg=(sg*w3+g[x]*w2+g[x-1]*w1+g[x-2])%mod; sh=(sh*w3+h[x]*w2+h[x-1]*w1+h[x-2])%mod; } return make_pair(num(sf,sg),num(sh,sg)); } }; const int G=1e5,T=1e9/G; int cv[T+128]; void read_factorial(){ int s=NumberOfNodes()-2,i,l,r,x; for(i=0;i<s;i++){ l=T*i/s+1,r=T*(i+1)/s; Receive(i+2); for(x=l;x<=r;x++)cv[x]=GetInt(i+2); } cv[0]=1; for(i=1;i<=T;++i)cv[i]=(i64)cv[i]*cv[i-1]%mod; } int fac(int n){ int res=cv[n/G],l=n/G*G; for(;l<n;res=(i64)res*(++l)%mod); return res; } num binom(int n,int m){return num(fac(n),(i64)fac(m)*fac(n-m)%mod);} int solve(int l,int r){ if(l>r)return 0; num pl(1,1),sum=pl; for(;l<r&&l%S;pl=pl*num(n-l,l+1),sum=sum+pl,++l); fp Q;Q.init(); int x=l/S; while(l+S<=r){ if(x>U)Q.mov(x),x=0; pair<num,num>g=Q.get(x); sum=sum+pl*g.second; pl=pl*g.first; l+=S;++x; } for(;l<r;pl=pl*num(n-l,l+1),sum=sum+pl,++l); return sum.value(); } int calc(int l,int r){ int res=1,x; for(x=l;x<=r;++x)res=(i64)res*x%mod; return res; } int ans; int main(){ int x,l,r,mid; mod=GetP(); if(MyNodeId()==0){ ans=0; n=GetN(),m=GetK(); if(m<=n/4){ l=0,r=m,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; }else if(m<=n/2){ l=m+1,r=n/2,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(fpow(2,n-1)+mod-ans)%mod; if(~n&1)ans=(ans+(i64)(mod/2+1)*binom(n,n>>1).value())%mod; }else if(m>=n-n/4){ l=m+1,r=n,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(fpow(2,n)+mod-ans)%mod; }else{ l=n/2+1,r=m,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(ans+fpow(2,n-1))%mod; if(~n&1)ans=(ans+(i64)(mod/2+1)*binom(n,n>>1).value())%mod; } printf("%d\n",ans); }else if(MyNodeId()==1){ Receive(0); n=GetInt(0),l=GetInt(0),r=GetInt(0); ans=solve(l,r); PutInt(0,ans),Send(0); }else{ n=NumberOfNodes()-2; m=MyNodeId()-2; l=T*m/n+1,r=T*(m+1)/n; for(x=l;x<=r;++x)PutInt(0,calc((x-1)*G+1,x*G)); Send(0); } }
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include "message.h" #include "futbol.h" #include "bits/stdc++.h" using namespace std; using i64=long long; using u32=unsigned int; using u64=unsigned long long; using pii=pair<int,int>; const int S=65,N=S+5,U=2048; int mod; template<class T>void exgcd(T a,T b,T&x,T&y){ if(!b)return (void)(x=1,y=0); exgcd(b,a%b,y,x),y-=a/b*x; } template<class T>T Inv(T a,T p){ T x,y;exgcd(a,p,x,y); return (x%p+p)%p; } int fpow(int a,int t){ int r; for(r=1;t;a=(i64)a*a%mod,t>>=1)if(t&1)r=(i64)r*a%mod; return r; } struct num{ int x,y; num(){x=0,y=1;} num(int x,int y):x(x),y(y){} friend num operator+(num a,num b){ i64 p=(i64)a.x*b.y+(i64)a.y*b.x; i64 q=(i64)a.y*b.y; return num(p%mod,q%mod); } friend num operator-(num a,num b){ i64 p=(i64)a.x*b.y-(i64)a.y*b.x; i64 q=(i64)a.y*b.y; return num((p%mod+mod)%mod,q%mod); } friend num operator*(num a,num b){ i64 p=(i64)a.x*b.x; i64 q=(i64)a.y*b.y; return num(p%mod,q%mod); } int value(){return (i64)x*Inv(y,mod)%mod;} }; int n,m; struct fp{ int f[N],g[N],h[N],c[N][N]; fp(){ memset(f,0,sizeof(f)),memset(g,0,sizeof(g)); memset(h,0,sizeof(h)),memset(c,0,sizeof(c)); } void init(){ int x,y; f[0]=g[0]=1; for(y=1;y<=S;++y){ for(x=y;x>=0;--x){ f[x]=(i64)f[x]*(n+mod-y+1)%mod; if(x)f[x]=(f[x]+(i64)f[x-1]*(mod-S))%mod; g[x]=(i64)g[x]*y%mod; if(x)g[x]=(g[x]+(i64)g[x-1]*S)%mod; h[x]=(i64)h[x]*y%mod; if(x)h[x]=(h[x]+(i64)h[x-1]*S)%mod; h[x]=(h[x]+f[x])%mod; } } for(x=0;x<=S;++x)for(y=c[x][0]=1;y<=x;++y)c[x][y]=(c[x-1][y]+c[x-1][y-1])%mod; } void mov(int w){ int x,y,p; for(x=1;x<=S;++x)for(y=x-1,p=w;y>=0;--y,p=(i64)p*w%mod){ f[y]=(f[y]+(i64)f[x]*p%mod*c[x][y])%mod; g[y]=(g[y]+(i64)g[x]*p%mod*c[x][y])%mod; h[y]=(h[y]+(i64)h[x]*p%mod*c[x][y])%mod; } } pair<num,num> get(u32 w){ int x; u64 sf=0,sg=0,sh=0; u64 w3=(u64)w*w*w,w2=w*w,w1=w; for(x=S;x>=0;x-=3){ sf=(sf*w3+f[x]*w2+f[x-1]*w1+f[x-2])%mod; sg=(sg*w3+g[x]*w2+g[x-1]*w1+g[x-2])%mod; sh=(sh*w3+h[x]*w2+h[x-1]*w1+h[x-2])%mod; } return make_pair(num(sf,sg),num(sh,sg)); } }; const int G=1e5,T=1e9/G; int cv[T+128]; void read_factorial(){ int s=NumberOfNodes()-2,i,l,r,x; for(i=0;i<s;i++){ l=T*i/s+1,r=T*(i+1)/s; Receive(i+2); for(x=l;x<=r;x++)cv[x]=GetInt(i+2); } cv[0]=1; for(i=1;i<=T;++i)cv[i]=(i64)cv[i]*cv[i-1]%mod; } int fac(int n){ int res=cv[n/G],l=n/G*G; for(;l<n;res=(i64)res*(++l)%mod); return res; } num binom(int n,int m){return num(fac(n),(i64)fac(m)*fac(n-m)%mod);} int solve(int l,int r){ if(l>r)return 0; num pl(1,1),sum=pl; for(;l<r&&l%S;pl=pl*num(n-l,l+1),sum=sum+pl,++l); fp Q;Q.init(); int x=l/S; while(l+S<=r){ if(x>U)Q.mov(x),x=0; pair<num,num>g=Q.get(x); sum=sum+pl*g.second; pl=pl*g.first; l+=S;++x; } for(;l<r;pl=pl*num(n-l,l+1),sum=sum+pl,++l); return sum.value(); } int calc(int l,int r){ int res=1,x; for(x=l;x<=r;++x)res=(i64)res*x%mod; return res; } int ans; int main(){ int x,l,r,mid; mod=GetP(); if(MyNodeId()==0){ ans=0; n=GetN(),m=GetK(); if(m<=n/4){ l=0,r=m,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; }else if(m<=n/2){ l=m+1,r=n/2,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(fpow(2,n-1)+mod-ans)%mod; if(~n&1)ans=(ans+(i64)(mod/2+1)*binom(n,n>>1).value())%mod; }else if(m>=n-n/4){ l=m+1,r=n,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(fpow(2,n)+mod-ans)%mod; }else{ l=n/2+1,r=m,mid=(l+r)>>1; PutInt(1,n),PutInt(1,mid+1),PutInt(1,r),Send(1); ans=solve(l,mid); read_factorial(); Receive(1); ans=((i64)ans*binom(n,l).value()+(i64)GetInt(1)*binom(n,mid+1).value())%mod; ans=(ans+fpow(2,n-1))%mod; if(~n&1)ans=(ans+(i64)(mod/2+1)*binom(n,n>>1).value())%mod; } printf("%d\n",ans); }else if(MyNodeId()==1){ Receive(0); n=GetInt(0),l=GetInt(0),r=GetInt(0); ans=solve(l,r); PutInt(0,ans),Send(0); }else{ n=NumberOfNodes()-2; m=MyNodeId()-2; l=T*m/n+1,r=T*(m+1)/n; for(x=l;x<=r;++x)PutInt(0,calc((x-1)*G+1,x*G)); Send(0); } } |