Bzoj thousand plan 309:bzoj4332:jsoi2012 snacks (divide and conquer FFT)

Source: Internet
Author: User

https://www.lydsy.com/JudgeOnline/problem.php?id=4332

Because if a child does not get candy, then the children behind her will not get candy.

So set g[i][j] to show the former I children, to the J Candy, and the former I children are divided into the program number of sweets

So that f (x) indicates the degree of happiness of x candies.

∴g[i][j] =∑g[i-1][j-k]*f (k)

Kee G[i]=g[i-1]*f, then g[i]=f ^ i

But the requirements are σg[i][m]

Kee F[n]=σg[i] i∈[1,n], then ans=f[n][m]

F[n]=σg[i] I∈[1,n]

=σf (N/2) +σg[i] i∈[n/2+1,n]

=σf (N/2) +σf^i I∈[n/2+1,n]

=σf (N/2) +σf^ (n/2+i) I∈[1,N/2]

=σf (N/2) +f^ (N/2) *σf^i I∈[1,N/2]

=σf (N/2) +g (N/2) *f (N/2)

Then you can divide and conquer the FFT solution

If n is odd, f (n) =f (n-1) +g[n]=f (n-1) +g (n-1) *f

Boundary Condition: g[][0]=1

#include <cmath>#include<cstdio>#include<algorithm>using namespacestd;Const intm=1<< -;#defineN 10001intM,mod;intr[m+1];intLen;Const DoublePi=acos (-1);structcomplex{Doublex, y; Complex () {} Complex (DoubleX_Doubley_): X (x_), Y (y_) {} Complexoperator+(Complex p) {Complex C; c.x=x+p.x; C.y=y+p.y; returnC; } Complexoperator-(Complex p) {Complex C; c.x=x-p.x; C.y=y-p.y; returnC; } Complexoperator*(Complex p) {Complex C; c.x=x*p.x-y*p.y; C.y=x*p.y+y*p.x; returnC; }    voidClear () {x=y=0; }};typedef Complex E; E f[m+1],f[m+1],g[m+1],tmp[m+1];voidFFT (E *a,intty) {     for(intI=0; i<len;++i)if(i<R[i]) swap (a[i],a[r[i]);  for(intI=1; i<len;i<<=1) {E wn (cos (pi/i), Ty*sin (pi/i));  for(intp=i<<1, j=0; j<len;j+=p) {E W (1,0);  for(intk=0; k<i;++k,w=w*WN) {E x=a[j+k],y=w*a[i+j+K]; A[j+k]=x+y; a[i+j+k]=x-y; }        }    }    if(ty==-1)    {         for(intI=0; i<len;++i) a[i].x=a[i].x/len,a[i].x=int(a[i].x+0.5)%mod,a[i].y=0; }}voidSolve (E *f,e *g,intN) {    if(!N) {g[0].x=1; return; }    if(n&1) {Solve (F,g,n-1); FFT (g,1);  for(intI=0; i<len;++i) g[i]=g[i]*F[i]; FFT (g,-1);  for(intI=0; i<=m;++i) f[i]=f[i]+G[i];  for(intI=0; i<=m;++i) f[i].x=int(f[i].x)%mod,f[i].y=0;  for(inti=m+1; i<len;++i) f[i].clear (), g[i].clear (); }    Else{Solve (f,g,n/2);  for(intI=0; i<len;++i) tmp[i]=F[i]; FFT (TMP,1); FFT (g,1);  for(intI=0; i<len;++i) tmp[i]=tmp[i]*G[i]; FFT (TMP,-1);  for(intI=0; i<len;++i) g[i]=g[i]*G[i]; FFT (g,-1);  for(intI=0; i<=m;++i) f[i]=f[i]+Tmp[i];  for(intI=0; i<=m;++i) f[i].x=int(f[i].x)%mod,f[i].y=0;  for(inti=m+1; i<len;++i) f[i].clear (), g[i].clear (); }}intMain () {intN,o,s,u; scanf ("%d%d%d%d%d%d",&m,&mod,&n,&o,&s,&u); //f[0].x=1;     for(intI=1; i<=m;++i) f[i].x= (o*i*i+s*i+u)%MoD; intL=0;  for(len=1; len<=m+m;len<<=1, l++);  for(intI=0; i<len;++i) r[i]= (r[i>>1]>>1)| ((i&1) <<l-1); FFT (F,1);    Solve (f,g,n); printf ("%d",int(f[m].x)); return 0;}

Bzoj thousand plan 309:bzoj4332:jsoi2012 snacks (divide and conquer FFT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.