Description

## 題解:

$$g[i][j]$$表示$$i$$個人里分下去了$$j$$個零食得到的值,n為人數,m為零食數

$$g[i][j]$$的遞推我們有
$g[i][j] = \sum_{k=1}^{j-1}g[i-1][j-k]*F(k)$

$f_n = f_{\frac{n}{2}} + \sum_{i=\frac{n}{2}+1}^{n}g_i$
$f_n = f_{\frac{n}{2}} + \sum_{i=1}^{\frac{n}{2}}g_{\frac{n}{2}+i}$

$$g_i = g_0*F^i$$可得:$$g_i*g_j = g_0*g_0*F^i*F^j$$

$f_n = f_{\frac{n}{2}} + \sum_{i=1}^{\frac{n}{2}}g_{\frac{n}{2}}*g_i$

$f_n = f_{\frac{n}{2}} + \sum_{i=1}^{\frac{n}{2}}\sum_{j=1}^{m-1}g_{\frac{n}{2},m-j}*g_{i,j}$
$f_n = f_{\frac{n}{2}} + \sum_{j=1}^{m-1}g_{\frac{n}{2},m-j}*\sum_{i=1}^{\frac{n}{2}}g_{i,j}$
$f_n = f_{\frac{n}{2}} + \sum_{j=1}^{m-1}g_{\frac{n}{2},m-j}*f_{\frac{n}{2},j}$
$f_n = f_{\frac{n}{2}} + g_{\frac{n}{2}}*f_{\frac{n}{2}}$

$g_n = g_{\frac{n}{2}}*g_{\frac{n}{2}}$

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 41000;
const double pi = acos(-1);
int mod;
struct complex{
long double x,y;
complex(){x=y=0;}
complex(long double a,long double b){x=a;y=b;}
complex operator + (const complex &r){return complex(x+r.x,y+r.y);}
complex operator - (const complex &r){return complex(x-r.x,y-r.y);}
complex operator * (const complex &r){return complex(x*r.x-y*r.y,x*r.y+y*r.x);}
complex operator / (const long double &r){return complex(x/r,y/r);}
};
inline void FFT(complex *x,int n,int p){
for(int i=0,t=0;i<n;++i){
if(i > t) swap(x[i],x[t]);
for(int j=n>>1;(t^=j) < j;j>>=1);
}
for(int m=2;m<=n;m<<=1){
int k = m>>1;
complex wn(cos(p*2*pi/m),sin(p*2*pi/m));
for(int i=0;i<n;i+=m){
complex w(1,0),u;
for(int j=0;j<k;++j,w=w*wn){
u = x[i+j+k]*w;
x[i+j+k] = x[i+j] - u;
x[i+j] = x[i+j] + u;
}
}
}
if(p == -1 ) for(int i=0;i<n;++i) x[i] = x[i]/n;
}
complex ca[maxn],cb[maxn],cc[maxn];int len,m;
inline int mul(int *a,int *b,int *c){
for(int i=0;i<len;++i){
ca[i] = complex((long double)a[i],0);
cb[i] = complex((long double)b[i],0);
}
FFT(ca,len,1);FFT(cb,len,1);
for(int i=0;i<len;++i) cc[i] = ca[i]*cb[i];
FFT(cc,len,-1);
for(int i=0;i<=m;++i){
c[i] = ((int)floor(cc[i].x + 0.5)) % mod;
}
}
int f[maxn],g[maxn],arr[maxn],tmp[maxn];
inline void qpow(int k){
if(k == 1){
for(int i=0;i<=m;++i) f[i] = g[i] = arr[i];
return ;
}qpow(k>>1);
mul(f,g,tmp);mul(g,g,g);
for(int i=0;i<=m;++i){
f[i] += tmp[i];
if(f[i] >= mod) f[i] -= mod;
}
if(k&1){
mul(g,arr,g);
for(int i=0;i<=m;++i){
f[i] += g[i];
if(f[i] >= mod) f[i] -= mod;
}
}
}
int main(){
for(len = 1;(len) <= (m<<1);len<<=1);
a %= mod;b %= mod;c %= mod;
for(int i=1;i<=m;++i) arr[i] = ((a*i*i % mod) + (b*i % mod) + c) % mod;
qpow(n);printf("%d\n",f[m]);
getchar();getchar();
return 0;
}

#### 注意！

© 2014-2022 ITdaan.com 联系我们：