【HDOJ】1502 Regular Words

大数+DP,感觉这个DP有点儿抽象,而且这个是大数,wa了很多次。

#include <stdio.h>

#define MAXNUM 61
#define MAXLEN 100

typedef struct {
    char val[MAXLEN];
} src_st;

src_st dp[MAXNUM][MAXNUM][MAXNUM];

void BigIntAdd(src_st *a, src_st *b) {
    int i;

    for (i=0; i<MAXLEN; ++i) {
        a->val[i] += b->val[i];
        if (a->val[i] >= 10) {
            a->val[i+1]++;
            a->val[i] -= 10;
        }
    }
}

int main() {
    int i, j, k;

    dp[0][0][0].val[0] = 1;

    for (i=1; i<MAXNUM; ++i)
        for (j=0; j<=i; ++j)
            for (k=0; k<=j; ++k) {
                memset(dp[i][j][k].val, 0, sizeof(dp[i][j][k].val));
                if (i-1>=j)
                    BigIntAdd(&dp[i][j][k].val, &dp[i-1][j][k].val);
                if (j-1>=k)
                    BigIntAdd(&dp[i][j][k].val, &dp[i][j-1][k].val);
                if (k)
                    BigIntAdd(&dp[i][j][k].val, &dp[i][j][k-1].val);
            }

    while (scanf("%d", &k) != EOF) {
        j = 0;
        for (i=MAXLEN-1; i>=0; i--) {
            if (j==0 && dp[k][k][k].val[i] == 0)
                continue;
            if (j==0 && dp[k][k][k].val[i] != 0)
                j = 1;
            printf("%c", dp[k][k][k].val[i]+'0');
        }
        printf("\n\n");
    }

    return 0;
}

 

posted on 2014-03-09 16:10  Bombe  阅读(203)  评论(0编辑  收藏  举报

导航