Test instructions
A word X is composed of {a,b,c} three letters.
A (x): The number of a in the word X. B (x), C (x) in the same vein.
A word X if it is regular word must satisfy a (x) =b (x) =c (x) and any prefix for X has a (x) >=b (x) >=c (x).
Give a number n. Ask how many regular word has a length of 3n.
Ideas:
DP[A][B][C]: The first a+b+c letters consist of a A, a, B, b,c c.
And then you can break it down,,,
*: Use high precision
Code:
int Constmaxn=99999999;int Constdlen=8;classbignum{Private: inta[ -]; intLen; Public: Bignum () {}; Bignum (Const int); Bignum&operator=(ConstBignum &); Bignumoperator+(ConstBignum &)Const; voidprint ();}; Bignum::bignum (Const intb) { intC,d=b; Len=0; Mem (A,0); while(d>MAXN) {C=d-(d/(maxn+1)) * (maxn+1); D=d/(maxn+1); A[len++]=C; } A[len++]=D;} Bignum& Bignum::operator=(ConstBignum &N) { inti; Len=N.len; Mem (A,0); Rep (I,0, len-1) a[i]=N.a[i]; return* This;} Bignum bignum::operator+(ConstBignum & T)Const{bignum T (* This); intI,big; Big=t.len>len?T.len:len; for(intI=0; i<big;++i) {T.a[i]+=T.a[i]; if(t.a[i]>MAXN) {T.a[i+1]++; T.a[i]-= (maxn+1); } } if(t.a[big]!=0) t.len=big+1;Elset.len=Big; returnt;}voidbignum::p rint () {inti; cout<<a[len-1]; for(i=len-2; i>=0; i--) {cout.width (Dlen); Cout.fill ('0'); cout<<A[i]; } cout<<Endl;}intN; Bignum dp[ +][ +][ +];intMain () {intN; while(SCANF ("%d", &n)! =EOF) {Mem (DP,0); dp[0][0][0]=bignum (1); Rep (A,0, N) {Rep (b,0, N) {Rep (c,0, N) { if(a==0&& b==0&& c==0)Continue; if(A>=b && b>=B) { if(A-1>=b && a>=1) {Dp[a][b][c]=dp[a][b][c]+dp[a-1][b][c]; } if(B-1>=c && b>=1) {Dp[a][b][c]=dp[a][b][c]+dp[a][b-1][c]; } if(c>=1) {Dp[a][b][c]=dp[a][b][c]+dp[a][b][c-1]; }}}}} dp[n][n][n].print (); printf ("\ n"); } return 0;}
Hdu 1502 Regular Words (DP)