HDU 5730 Shell Necklace (CDQ Division +fft)

Source: Internet
Author: User
Tags mul

"Topic link" http://acm.hdu.edu.cn/showproblem.php?pid=5730

"The main topic"

Give An array w, representing the weights of the fields of different lengths, such as W[3]=5, if the field length is 3, then its weight is 5, and now there is a field of length n, the sum of the values of the field weights obtained by different splits.

Exercises

  Note Dp[i] is the answer to the length of I, dp[i]=sum_{j=0}^{i-1}dp[j]w[i-j], found to be a convolution of the formula, so the operation can be optimized by FFT, but because in the calculation process Dp[j] is an unknown value, the sequential computational complexity is O ( N2LOGN), considering that the addition operation can be allocated to the multiplication operation, it is possible to take CDQ division, using recursive statistics to the left DP value in each interval contribution to the right DP value, the calculation of each contribution value is optimized by FFT, optimizing the time complexity to O (NLOGNLOGN).

Code

#include <cstdio> #include <cmath> #include <algorithm>using namespace std;typedef long Long ll;const        int n=524300,p=313;int n,pos[n];namespace fft{struct comp{double r,i;        Comp (double _r=0,double _i=0): R (_r), I (_i) {} comp operator + (const comp&x) {return comp (r+x.r,i+x.i);}        Comp operator-(const comp&x) {return comp (r-x.r,i-x.i);}        Comp operator * (const comp&x) {return comp (r*x.r-i*x.i,i*x.r+r*x.i);}    Comp Conj () {return comp (r,-i);}    }a[n],b[n];    Const double Pi=acos (-1.0);        void FFT (comp a[],int n,int t) {for (int i=1;i<n;i++) if (pos[i]>i) swap (a[i],a[pos[i]]);            for (int d=0; (1<<d) <n;d++) {int m=1<<d,m2=m<<1;            Double o=pi*2/m2*t;            Comp _w (cos (o), sin (o));                for (int i=0;i<n;i+=m2) {Comp W (1,0);                    for (int j=0;j<m;j++) {comp& a=a[i+j+m],&b=a[i+j],t=w*a;  A=b-t;                  B=b+t;                W=w*_w;    }}}if (T==-1) for (int i=0;i<n;i++) a[i].r/=n;        } void Mul (int *a,int *b,int *c,int k) {int i,j;        for (i=0;i<k;i++) A[i]=comp (A[i],b[i]);        J=__builtin_ctz (k)-1; for (int i=0;i<k;i++) {pos[i]=pos[i>>1]>>1| (         (i&1) <<j);}        FFT (a,k,1);            for (int i=0;i<k;i++) {j= (k-i) & (K-1);        B[i]= (a[i]*a[i]-(A[j]*a[j]). CONJ ()) *comp (0,-0.25);        }fft (b,k,-1);    for (int i=0;i<k;i++) c[i]= (Long Long) (b[i].r+0.5)%P; }}int w[n],a[n],b[n],c[n],f[n];void CDQ (int l,int r) {if (l==r) {f[l]+=w[l];    F[l]%=p;return;}    int mid= (L+R) >>1; CDQ (L,mid);    int n=1;    while (n<r-l) n<<=1;    for (int i=0;i<=mid-l;i++) a[i]=f[i+l];    for (int i=mid-l+1;i<n;i++) a[i]=0;    for (int i=0;i<r-l;i++) b[i]=w[i+1];    for (int i=r-l;i<n;i++) b[i]=0;    Fft::mul (A,b,c,n); for (int i=mid+1;i<=r;i++) {f[i]+=c[i-l-1];    F[i]%=p; }CDQ (mid+1,r);}        int main () {while (~SCANF ("%d", &n) &&n) {f[0]=1;            for (int i=1;i<=n;i++) {scanf ("%d", &w[i]);            W[i]%=p;        f[i]=0;        }CDQ (1,n);    printf ("%d\n", F[n]); }return 0;}

  

HDU 5730 Shell Necklace (CDQ Division +fft)

Related Article

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.