# dp--dhu1502

http://blog.csdn.net/jiang199235jiangjj/article/details/7452389

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.

dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1]；同时注意，dp的结果很大，要用到大数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#define N 80
using namespace std;

char num[62][N],dp[62][62][62][N];

void sum(char a[N],char b[N])
{
int n=a[0],m=b[0],i,j,k;
a[0]=k=max(n,m);
for(i=1;i<=k;i++)
{
a[i]+=b[i];
if(a[i]>9)
{
a[i+1]++;			a[i]%=10;
if(i+1>k)
{
k++;a[0]++;
}
}
}
}
void cpy(char a[N],char b[N])
{
int i;
for(i=0;i<=b[0];i++)
{
a[i]=b[i];
}
}
{
if(x-1>=0&&x-1>=y&&y>=z)
sum(dp[x][y][z],dp[x-1][y][z]);
if(y-1>=0&&x>=y-1&&y-1>=z)
sum(dp[x][y][z],dp[x][y-1][z]);
if(z-1>=0&&x>=y&&y>=z-1)
sum(dp[x][y][z],dp[x][y][z-1]);
}
void fun()
{
int i,j,k;
memset(dp,0,sizeof(dp));
dp[0][0][0][0]=dp[0][0][0][1]=1;
for(i=1;i<=60;i++)
for(j=0;j<=i;j++)
for(k=0;k<=j;k++)
{
if(i==j&&j==k)
{
cpy(num[i],dp[i][j][k]);
}
}
}
int main()
{
fun();
int n;
while(~scanf("%d",&n))
{
for(int i=num[n][0];i>0;i--)
printf("%d",num[n][i]);
printf("\n\n");
}
}

• 本文已收录于以下专栏：

## hdu 1502 , poj 1463, hdu 1561 树形dp

hdu 1502 Anniversary party http://acm.hdu.edu.cn/showproblem.php?pid=1520    选取u点，最大值为 u的所有子节点都不取...

## HDU 1502 dp + 大数

dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1]；同时注意，dp的结果很大，要用到大数。 设sum[i]表示当n为i时的结果，即当i...
• vsooda
• 2012年09月05日 23:22
• 532

## 暑期dp46道(28) hdoj 1502 Regular Words dp+高精度额

• 2016年08月11日 14:49
• 146

## FFT 快速傅立叶变换 dhu1402 A*B

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

## dhu 1180 诡异的楼梯

#include #include #include #include using namespace std; char Map[25][25]; int vist[25][25]; i...