【DP】HDU 2189 悼念512汶川大地震遇难同胞——来生一起走

打一个小于150的素数表

为了分成的组的人数不重复用dp[i][j] 表示

i表示i个人 j表示分成的组内的最大素数序号


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 2111;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
int prim[222],dp[222][222];
bool yes[155];
int main()
{
    int tol=0;
    memset(yes,false,sizeof(yes));
    for(int i=2; i<=150; i++)
    {
        if(!yes[i])
        {
            prim[tol++]=i;
            for(int j=i+i; j<=150; j+=i)
                yes[j]=true;
        }
    }
    memset(dp,0,sizeof(dp));
    for(int i=0; i<tol; i++)
        dp[0][i]=1;
    for(int i=0; i<=150; i++)
    {
        for(int k=0; k<tol; k++)
        {
            for(int j=0; j<=k; j++)
            {

                int x=i+prim[j];
                if(x<=150)
                    dp[x][k]+=dp[i][j];
            }
        }
    }
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);

        printf("%d\n",dp[n][tol-1]);
    }
    return 0;
}

再次放错顺序wa掉了 囧rz

一维方程

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 2111;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
int prim[222],dp[222];
bool yes[155];
int main()
{
    int tol=0;
    memset(yes,false,sizeof(yes));
    for(int i=2; i<=150; i++)
    {
        if(!yes[i])
        {
            prim[tol++]=i;
            for(int j=i+i; j<=150; j+=i)
                yes[j]=true;
        }
    }
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        int tr=0;
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        dp[0]=1;

        for(int j=0; j<tol; j++)
        {
            for(int i=0; i<=n; i++)
            {
                int x=i+prim[j];
                if(x<=n)
                    dp[x]+=dp[i];
            }
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}






posted @ 2014-09-01 10:39  kewowlo  阅读(167)  评论(0)    收藏  举报