HDU 5730-shell Necklace

Source: Internet
Author: User

Test instructions

Give a continuous coloring method of 1-n beads A[i] (1<=i<=n), ask how many coloring schemes are in the bead chain of length n

Analysis:

The DP equation can be obtained: Dp[n] =∑ (i=1,n) (Dp[n-i]*a[i]).

The equation is convolution form, so CDQ + FFT

  

CDQ: Will [L,r] two points, first get [L,mid] answer, and then update [L,MID] contribution to [mid+1,r].

For any dp[j] (mid+1 <= J <= R), [L,mid] contributes to ∑ (I=l,mid) (Dp[i]*a[j-i]), that is, the polynomial DP and a phase after the number of J.

FFT: Optimizes the multiplication of polynomial.

(1 and L can not see the same broken blog park, the code is still sticky good, = =)

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const DoublePI =4* ATAN (1.0);7 Const intMAXN =200005;8 Const intMOD =313;9 structComplexTen { One     Doublex, y; AComplex (Doublexx =0.0,Doubleyy =0.0): X (xx), Y (yy) {} -Complexoperator- (ConstComplex &b)Const  -     { the         returnComplex (x-b.x, Y-b.y); -     } -Complexoperator+ (ConstComplex &b)Const -     { +         returnComplex (x + b.x, y +b.y); -     } +Complexoperator* (ConstComplex &b)Const A     { at         returnComplex (x*b.x-y*b.y, X*b.y + y*b.x); -     } - }; - voidChange (Complex y[],intlen) - { -     intI, J, K; in      for(i =1, j = len/2; I < len-1; i++) -     { to         if(I <j) Swap (Y[i], y[j]); +K = len/2; -          while(J >=k) the         { *J-=K; $K/=2;Panax Notoginseng         } -         if(J < k) J + =K; the     } + } A voidFFT (Complex y[],intLenintOn ) the { + Change (y, len); -      for(inth =2; H <= Len; H <<=1) $     { $Complex wn (cos (-on*2*pi/h), sin (-on*2*pi/h)); -          for(intj =0; J < Len; J + =h) -         { theComplex W (1,0); -              for(intK = J; K < J + h/2; k++)Wuyi             { theComplex U =Y[k]; -Complex t = w * y[k + h/2]; WuY[k] = U +T; -Y[k + h/2] = U-T; Aboutw = w *WN;  $             } -         } -     }  -     if(On =-1) A          for(inti =0; i < Len; i++) +y[i].x/=Len; the } - intt, N; $ Complex X[MAXN], Y[MAXN]; the inta[maxn/2], dp[maxn/2]; the voidCDQ (intLintR) the { the     if(L = = r) {Dp[l] = (Dp[l] + a[l])% MOD;return; }  -     intMid = (L + r) >>1;  inCDQ (L, mid);//processing the first half of the paragraph the     intLen =1, len1 = Mid-l +1, Len2 = R-l +1; the      while(Len < len2) Len <<=1; About      for(inti =0; i < len1; i++) X[i] = Complex (Dp[i + l],0); the      for(inti = len1; i < Len; i++) X[i] = Complex (0,0); the      for(inti =0; i < len2; i++) Y[i] = Complex (A[i],0); the      for(inti = len2; i < Len; i++) Y[i] = Complex (0,0);  +FFT (x, Len,1); -FFT (y, Len,1); the      for(inti =0; i < Len; i++) X[i] = x[i] *Y[i];BayiFFT (x, Len,-1); the      for(inti = mid+1; I <= R; i++)//Update Contributions the     { -Dp[i] = (int) (Dp[i] + x[i-l].x +0.5) %MOD; -     } theCDQ (Mid +1, R);//processing the latter half of the paragraph the } the intMain () the { -      while(~SCANF ("%d", &n) &&N) the     { the          for(inti =1; I <= N; i++) the         {94scanf"%d",&a[i]); theA[i]%=MOD; theDp[i] =0; the         }98CDQ (1, n); Aboutprintf"%d\n", Dp[n]); -     }101}

HDU 5730-shell Necklace

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.