hdu 1502 大数dp

原创 2015年11月19日 21:07:07

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1502


#include  <stdio.h>
#include <string.h>

char dp[65][65][65][80];
int i,j,k;

int max(int a,int b)
{
    if(a > b)
        return a;
    return b;
}

void add(char a[],char b[],char c[])
{
    int len1,len2,len;
    int z,up,tmp;

    len1 = strlen(a);
    len2 = strlen(b);
    len = max(len1,len2);
    up = 0;

    for(z = 0; z < len; z++)
    {
        tmp = 0;
        if(z < len1)
            tmp +=(a[z]-'0');
        if(z < len2)
            tmp +=(b[z]-'0');
        if(up)
            tmp++;
        if(tmp>9)
        {
            up = 1;
            tmp-=10;
        }
        else up = 0;
        c[z] = tmp+'0';
    }

    if(up)
        c[len++] = '1';
    c[len] = '\0';
    strcpy(dp[i][j][k],c);
}

int main()
{
    int n;

    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(k-1>=0)
                        add(dp[i][j][k-1],dp[i][j][k],dp[i][j][k]);
                }
            }
        }

        while(~scanf("%d",&n))
        {
            i = strlen(dp[n][n][n]);
            i--;
            for(; i >= 0; i--)
                printf("%c",dp[n][n][n][i]);
            printf("\n\n");
        }
    
    return 0;
}


打表


#include <stdio.h>
#include <string.h>

char dp[65][85] = {
"1",
"1",
"5",
"42",
"462",
"6006",
"87516",
"1385670",
"23371634",
"414315330",
"7646001090",
"145862174640",
"2861142656400",
"57468093927120",
"1178095925505960",
"24584089974896430",
"521086299271824330",
"11198784501894470250",
"243661974372798631650",
"5360563436201569896300",
"119115896614816702500900",
"2670926804331443293626900",
"60386171228363065768956000",
"1375596980582110638216817680",
"31554078431506568639711925552",
"728440733705121725605657358256",
"16916012593818937850175820875056",
"394984727560107218767652172156480",
"9269882950945137003216002357575872",
"218589820552932101591964442689934272",
"5177405669064206309480641678873685136",
"123139887106265725065261170839575261246",
"2940211742938376804365727956142799686970",
"70461309651358512358741033490151564263034",
"1694426732092192797198296281548882854896770",
"40879953049935966764838175153044218787509460",
"989318124094680800242093703952690318964293660",
"24011992526103689868224096174884123328708261100",
"584414956558400574946623386902564355477176447080",
"14261150342358043298392602404780869211095488665940",
"348876433985002864104580005170614922408018905657020",
"8555006509113973886896694412506009110609925390878620",
"210257823823361408953856390159370731312558948560177500",
"5178713915261459187808923452167773648813573133021584000",
"127816663734641521693312994768720558317819058630953008000",
"3160890723051037742300958639363743464856851891194511344000",
"78316111638147520232116305011469771592038383559489541704000",
"1943917771018304520047172570820410402016667020494472553010000",
"48334523581589010102952513742546024844918906756931542442556400",
"1203813957908516875152358489329058054078745007110871474716375280",
"30029983483935083858438698423851117882968874317657169412268673840",
"750270153399794678576435057573545926324276055884108148422050727840",
"18772482769028405636917719941593858764528793976890630506115671775200",
"470373947038907707302405010980987131831213397364392909428995307126880",
"11802109943885320655951253002795677125946808879324767545672973160638080",
"296516920131524804299707608337156053506400465189952712435084509896783040",
"7459203321130790040650176332416188852363369960068846727881499803410725440",
"187875141510304732204453155491218970539216498205240765481036372897711988800",
"4737637890492057297860769571861620074038072983555206964113320603342642320960",
"119605940186192921945993199027326146131452990076639651225155962772912609414400",
"3022912056752362939484322031260179006906680462576858197252183463144268821651200",
};

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        printf("%s\n\n",dp[n]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

hdu 1502 DP

开始一直以为是数学,要推出递推公式。。。然后看了别人的代码才发现是DP 然后就用记忆化搜索做了 用了大数的模版 dp[i][j][k]=  dp[i-1][j][k](最后一个字母为A,)+ d...

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

【DP】HDU-1502 Regular Words

给你三个字符ABC,设字符串中ABC的个数相等,再给你每个字符的个数,问在下列条件下,能形成几种排列:前i个字符中,n(A) >= n(B) >= n(C)。即A的个数大于等于B的个数大于等于C的个数...

hdu 1502 , poj 1463, hdu 1561 树形dp

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

HDU-4359-DP+C(n, m)大数求

比赛的时候一直在敲这道题。一开题就盯住这个名字了。有人说他说是 treeDP 不一定真的是 treeDP。 总有时候别人会给你很诚恳的忠告。却只因自己陷得太深无法自拔。关键是自己不想出去。 跑题了...

HDU 5568 sequence2(大数+DP)

题意:给一个序列,问有多少个长度为k的上升子序列(对i 思路:看到上升子序列应想到dp。dp[i][j]表示以a[i]结尾长度为j的上升子序列有多少个。那么得到转移方程:dp[i][j] = su...

hdu5568 sequence2(dp+大数)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5568 题目大意: 给一段长度为n的序列,现在在其中取k个,问这k个是递增的取法有多少种。 范围:n...

HDU 5568 dp+大数板子

sequence2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota...

dp--dhu1502

http://blog.csdn.net/jiang199235jiangjj/article/details/7452389 Consider words of length 3n over al...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)