HDU Shell Necklace CDQ Division +fft

Source: Internet
Author: User

Shell Necklace

problem DescriptionPerhaps the sea ' s definition of a shell is the pearl. However, in my view, a shell necklace with n beautiful shells contains the most sincere feeling for my best lover Arrietty , but even that's not enough.

Suppose the shell necklace is a sequence of shells (not a chain end to end). Considering I continuous shells in the shell necklace, I know that there exist different schemes to decorate the I shells Together with one declaration of love.

I want to decorate all the shells with some declarations of love and decorate each shell just one time. As a problem, I want to know the total number of schemes. InputThere is multiple test cases (no more than -Cases and no more than 1 in extreme case), ended by 0.

For each test cases, the first line contains an integerN, meaning the number of shells in this shell necklace, where1≤n≤5 . Following line was a sequence withNNon-negative integera1,< Span id= "mathjax-span-29" class= "Msubsup" >a2, ... , An , andai≤7 Meaning the number of schemes to decoratei continuous shells together with a declaration of love.
OutputFor each test case, print one line containing the total number of schemes module 313(three hundred and thirteen implies the March 13th, a special and purposeful day). Sample Input31 3 742 2 2 2 0 Sample Output1454 HintFor the first test case in Sample Input, the Figure 1 provides all schemes about it. The total number of schemes is 1 + 3 + 3 + 7 = 14.

Exercises

F[i] =∑f[n-i] * A[i]

Divide and conquer the FFT classics without repeating them

#include <bits/stdc++.h>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF = 1e18+1LL;Const DoublePI = ACOs (-1.0);Const intN = 3e5+Ten, M = 1e3+ -, INF =2e9;ConstLL mod =313LL;structComplex {DoubleR, I; Complex () {} Complex (DoubleRDoublei): R (R), I (i) {} Complexoperator+ (Constcomplex& t)Const {        returnComplex (R + T.R, i +t.i); } Complexoperator- (Constcomplex& t)Const {        returnComplex (R-T.R, I-t.i); } Complexoperator* (Constcomplex& t)Const {        returnComplex (R * t.r-i * t.i, R * t.i + i *T.R); }} ;voidFFT (Complex y[],intNintrev) {     for(inti =1, J, T, K; I < n; ++i) { for(j =0, t = i, k = n >>1; K K >>=1, T >>=1) J = J <<1| T &1 ; if(I <j) Swap (Y[i], y[j]); }     for(ints =2, ds =1; s <= N; DS = s, S <<=1) {Complex WN= Complex (cos (rev *2* pi/s), sin (rev *2* pi/s)), W (1,0), t;  for(intK =0; K < DS; + + K, w = w *WN) {             for(inti = k; I < n; i + =s) {y[i+ ds] = Y[i]-(t = w * y[i +DS])                ; Y[i]= Y[i] +T; }        }    }    if(Rev = =-1) for(inti =0; I < n; + + i) Y[I].R/=N;} Complex Y[n],s[n]; LL Dp[n],a[n];intN;voidCdqintllintRR) {     if(LL = =RR) {Dp[ll]+=A[ll]; DP[LL]%=MoD; return ;    } CDQ (Ll,mid); intLen =1;  while(Len <= (rr-ll+2)) len<<=1;  for(inti =0; i < Len; ++i) Y[i] = Complex (0,0), s[i] =Y[i];  for(inti =0; i < Len; ++i) Y[i] = Complex (a[i+1],0);  for(inti = ll; I <= mid; ++i) S[i-ll] = Complex (Dp[i],0); FFT (S,len,1); FFT (Y,len,1);  for(inti =0; i < Len; ++i) S[i] = y[i] *S[i]; FFT (S,len,-1);  for(inti = mid; I < RR; ++i) dp[i+1] + = LL (s[i-ll].r+0.5), Dp[i]%=MoD; CDQ (Mid+1, RR);}intMain () { while(SCANF ("%d", &n)! =EOF) {        if(!n)return 0; Memset (A,0,sizeof(a));  for(inti =1; I <= N; ++i) scanf ("%lld", &a[i]), dp[i] =0, a[i] = a[i]%MoD; CDQ (1, N); printf ("%lld\n", Dp[n]); }    return 0;}

HDU 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.