"JSOI2012" Snack generation function FFT

Source: Internet
Author: User

we construct $f (x) $ of generating function $g (x) $, then obviously $[x^k]g (x) =ok^2+sk+u$

So obviously, the answer is $\sum_{i=1}^{n} [X^m]g^i (x) $

We construct the generating function of the answer $f (x) =\sum_{i=1}^{n} g^i (x) $

According to the geometric series summation formula, $F (x) =\dfrac{1-g (x)}{1-g^{a+1} (x)}$

If you go to geometric series sum, you need polynomial fast power + polynomial inversion, time complexity is obviously $o (m\ log\ m) $.

However, this modulus is not a prime number, so it is not very good to engage.

We can calculate the value of $\sum_{i-1}^{2^k-1}g^i (x) $ in a way that is similar to a fast power.

The time complexity is obviously $o (m\ log\ m\ log\ A) $.

And then it's gone.

The first time I launch a generating function of the problem flattered

1#include <bits/stdc++.h>2 #defineMOD 9982443533 #defineL Long Long4 #defineM 1<<155 #defineG 36 using namespacestd;7 8 l Pow_mod (l x,l k) {9L ans=1;Ten      while(k) { One         if(k&1) ans=ans*x%MOD; AX=x*x%mod; k>>=1; -     } -     returnans; the } - voidChange (L a[],intN) { -      for(intI=0, j=0; i<n-1; i++){ -         if(i<j) Swap (A[i],a[j]); +         intK=n>>1; -          while(j>=k) j-=k,k>>=1; +j+=K; A     } at } - voidNTT (L a[],intNintOn ) { - Change (a,n); -      for(intH=2; h<=n;h<<=1){ -L Wn=pow_mod (G, (mod-1)/h); -          for(intj=0; j<n;j+=h) { inL w=1; -              for(intk=j;k<j+ (h>>1); k++){ toL u=a[k],t=w*a[k+ (h>>1)]%MOD; +a[k]= (u+t)%MOD; -a[k+ (h>>1)]= (u-t+mod)%MOD; thew=w*wn%MOD; *             } $         }Panax Notoginseng     } -     if(on==-1){ theL Inv=pow_mod (n,mod-2); +          for(intI=0; i<n;i++) a[i]=a[i]*inv%MOD; AReverse (A +1, A +n); the     } + } - L m,p,a,o,s,u; $L g[m]={0},gsum[m]={0},ans[m]={0}; $  - intMain () { -Cin>>m>>p>>a>>o>>s>>U; the      for(L i=1; i<=m;i++) g[i]= (o*i*i+s*i+u)%P; -     intlen=1; while(Len<= (m*2)) len<<=1;Wuyigsum[0]=1; theA=min (a,m); -      while(A) { Wu          -         if(a&1){ AboutNTT (Ans,len,1); NTT (G,len,1); $              for(intI=0; i<len;i++) ans[i]=ans[i]*g[i]%MOD; -NTT (ans,len,-1); NTT (g,len,-1); -              for(intI=1; i<=m;i++)  -Ans[i]= (Ans[i]+g[i]+gsum[i])%P; A              for(inti=m+1; i<len;i++) ans[i]=0;  +         } thea>>=1; -          $g[0]++; theNTT (G,len,1); NTT (Gsum,len,1); the          for(intI=0; i<len;i++) gsum[i]=gsum[i]*g[i]%MOD; theNTT (g,len,-1); NTT (gsum,len,-1); theg[0]--; -          for(intI=0; i<len;i++)if(i>m) gsum[i]=0;Elsegsum[i]%=P; in          theNTT (G,len,1);  the          for(intI=0; i<len;i++) g[i]=g[i]*g[i]%MOD; AboutNTT (g,len,-1); the          for(intI=0; i<len;i++)if(i>m) g[i]=0;Elseg[i]%=P; the     } thecout<<ans[m]<<Endl; +}

"JSOI2012" Snack generation function 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.