HDU1502:Regular Words(大数DP)

Problem Description
Consider words of length 3n over alphabet {A, B, C} . Denote the number of occurences of A in a word a as A(a) , analogously let the number of occurences of B be denoted as B(a), and the number of occurenced of C as C(a) .  

Let us call the word w regular if the following conditions are satisfied:  

A(w)=B(w)=C(w) ;  
if c is a prefix of w , then A(c)>= B(c) >= C(c) .  
For example, if n = 2 there are 5 regular words: AABBCC , AABCBC , ABABCC , ABACBC and ABCABC .  

Regular words in some sense generalize regular brackets sequences (if we consider two-letter alphabet and put similar conditions on regular words, they represent regular brackets sequences).  

Given n , find the number of regular words.
 

Input
There are mutiple cases in the input file.  

Each case contains n (0 <= n <= 60 ).  

There is an empty line after each case.
 

Output
Output the number of regular words of length 3n .  

There should be am empty line after each case.
 

Sample Input
   
   
   
   
2 3
 

Sample Output
   
   
   
   
5 42


 

题意:就是求ABC每个字母有n个,求排列的个数

思路:dp思想,按照题目要求的排列的方式,得出公式dp[i][j][k] = dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1],并且i>=j>=k.但是要注意这些数字到了后面将会越来越大,所以必须要用大数模拟

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

char dp[65][65][65][85];

void add(char a[],char b[],char back[])
{
    int i,j,k,up,x,y,z,l;
    char *c;
    if(strlen(a) > strlen(b))
        l = strlen(a)+2;
    else
        l = strlen(b)+2;
    c = (char*)malloc(l*sizeof(char));
    i = strlen(a)-1;
    j = strlen(b)-1;
    k = 0;
    up = 0;
    while(j>=0 || i>=0)
    {
        if(i<0) x = '0';
        else
            x = a[i];
        if(j<0) y = '0';
        else
            y = b[j];
        z = x-'0'+y-'0';
        if(up)
            z++;
        if(z>9)
        {
            up = 1;
            z%=10;
        }
        else
            up = 0;
        c[k++] = z+'0';
        i--;
        j--;
    }
    if(up)
        c[k++] = '1';
    i = 0;
    c[k] = '\0';
    for(k-=1; k>=0; k--)
        back[i++] = c[k];
    back[i] = '\0';
}

int main()
{
    int n,i,j,k;
    for(i = 0; i<=60; i++)
        for(j = 0; j<=60; j++)
            for(k = 0; k<=60; k++)
                strcpy(dp[i][j][k],"0");
    strcpy(dp[1][0][0],"1");
    strcpy(dp[1][1][0],"1");
    strcpy(dp[1][1][1],"1");
    for(i = 2; i<=60; i++)
    {
        for(j = 0; j<=i; j++)
        {
            for(k = 0; k<=j; k++)
            {
                if(i-1>=j)
                    add(dp[i-1][j][k],dp[i][j][k],dp[i][j][k]);
                if(j-1>=k)
                    add(dp[i][j-1][k],dp[i][j][k],dp[i][j][k]);
                if(j>=k-1)
                    add(dp[i][j][k],dp[i][j][k-1],dp[i][j][k]);
            }
        }
    }
    while(~scanf("%d",&n))
    {
        printf("%s\n\n",dp[n][n][n]);
    }

    return 0;
}


 

你可能感兴趣的:(dp,高精度,hnu)