HDU5730 Shell Necklace

Source: Internet
Author: User
Tags modulus

Time limit:16000/8000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 999 Accepted Submission (s): 434


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.

Authorhit

Source2016 multi-university Training Contest 1

Recommendwange2014

A long x necklace as a whole, with a[x] kind of decoration scheme. It is possible to connect the different whole together. Ask how many options there are for a necklace of length n.

Dynamic programming for the division and treatment of FFT

Set F[i] As the number of schemes long as I, obviously $ f[i]=\sum_{j=1}^{i} A[j]*f[i-j] $

Modulus is 313, what is the original root of this number? I don't know. The modulus is so small that it can be used with an FFT.

PS1 attention to read into the a[] when it is easy to take the mold, or it will easily explode

PS2 I don't know why I want to output a newline character, White WA took three times to see

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 #defineLL Long Long7 using namespacestd;8 Const DoublePi=acos (-1.0);9 Const intMod=313;Ten Const intmxn=200010; One intRead () { A     intx=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten-'0'+ch;ch=GetChar ();} the     returnx*F; - } - structcom{ -     Doublex, y; +Comoperator+ (Constcom &b) {return(COM) {x+b.x,y+b.y};} -Comoperator- (Constcom &b) {return(COM) {x-b.x,y-b.y};} +Comoperator* (Constcom &b) {return(COM) {x*b.x-y*b.y,x*b.y+y*b.x};} AComoperator/ (Const DoubleV) {return(COM) {x/v,y/v};} at}a[mxn<<2],b[mxn<<2]; - intn,len,rev[mxn<<2]; - voidFFT (COM *a,intflag) { -      for(intI=0; i<n;i++)if(i<Rev[i]) swap (a[i],a[rev[i]); -      for(intI=1; i<n;i<<=1){ -COM wn= (COM) {cos (pi/i), Flag*sin (pi/i)}; in         intp=i<<1; -          for(intj=0; j<n;j+=p) { toCOM w= (COM) {1,0}; +              for(intk=0; k<i;k++,w=w*WN) { -COM x=a[j+k],y=w*a[j+k+i]; thea[j+k]=x+y; *a[j+k+i]=x-y; $             }Panax Notoginseng         } -     } the     if(flag==-1) +          for(intI=0; i<n;i++) a[i].x/=N; A     return; the } + intN,W[MXN]; - LL F[MXN]; $ voidSolveintLintR) { $     if(L==R) {(f[l]+=w[l])%=mod;return;} -     intMid= (l+r) >>1; - solve (l,mid); the     intI,j,m= (r-l+1); -      for(n=1, len=0; n<=m; n<<=1)++Len;Wuyi      for(i=0; i<n;i++) rev[i]= (rev[i>>1]>>1)| ((i&1) << (len-1)); the     // -      for(i=l;i<=mid;i++) {a[i-l]= (COM) {f[i],0};} Wu      for(i=mid-l+1; i<n;i++) a[i]= (COM) {0,0}; -      for(i=0; i<n;i++) b[i]= (COM) {w[i+1],0}; About     // $FFT (A,1); FFT (b,1); -      for(i=0; i<n;i++) a[i]=a[i]*B[i]; -FFT (a,-1); -      for(i=mid+1; i<=r;i++){ A(f[i]+= (LL) (a[i-l-1].x+0.5))%mod)%=MoD; +     } theSolve (mid+1, R); -     return; $ } the intMain () { the     inti,j; the      while(SCANF ("%d", &n)!=eof &&N) { theMemset (F,0,sizeoff); -          for(i=1; i<=n;i++) W[i]=read ()%mod;// inSolve1, n); theprintf"%lld\n", f[n]%MoD); the     } About     return 0; the}

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