hdu 1502 大数dp

对于每一个dp的问题 从其最优解的结构(分哪几种形式或者情况)入手 然后分析状态 这样就比较好找出状态转方程这里数据结构的选择很简单 顺序数组就可以 填充的方式顺序填充就可以 

然后这道题目卡了我大数。。 

#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;
}  
时间: 2024-10-22 19:01:24

hdu 1502 大数dp的相关文章

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000

hdu 4123 树形DP+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=4123 Problem Description Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses,

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

hdu 1087 简单dp

思路和2391一样的.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; const int inf=(0x7f7f7f7f); int main() { int a; int s[10005]; int w[10005];

HDU 2845 Beans(DP,最大不连续和)

题意    吃豆子游戏    当你吃了一个格子的豆子   该格子左右两个和上下两行就不能吃了    输入每个格子的豆子数    求你最多能吃多少颗豆子 可以先求出每行你最多可以吃多少颗豆子   然后每行就压缩成只有一个格子了   里面的豆子数就是那一行最多可以吃的豆子数   然后问题就变成求一列最多可以吃多少颗豆子了   和处理每一行一样处理   那么问题就简化成求一行数字的最大不连续和问题了 令d[i]表示某一行前i个豆子的最大和  有两种情况  吃第i个格子中的豆子和不吃第i个格子中的豆子

hdu 1250 树形DP

Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-27) Description There is going to be a party to celebrate the 80-th Anniversary of the Ural St

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew