5 POJ 3132 Sum of Different Primes

dp[j][k]表示用j个质数表示k这个值得方法数。

一个质数只能用一次,类似01背包。

#include<cstdio>
#include<cstring>
const int maxn=1130;
int pri[maxn+10],dp[20][maxn+10];
void init()
{
    int i,j;
    for(i=0;i<=maxn;i++) pri[i]=1;
    pri[0]=pri[1]=0;
    for(i=2;i<=maxn;i++)
    {
        if(!pri[i]) continue;
        for(j=i+i;j<=maxn;j+=i)
            pri[j]=0;
    }
}
int main()
{
    init();
    int i,j,k,m,n;
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        memset(dp,0,sizeof dp);
        dp[0][0]=1;
        for(i=2;i<=n;i++)
        {
            if(!pri[i]) continue;
            for(k=n;k>=i;k--)
                for(j=m;j>0;j--)
                    dp[j][k]+=dp[j-1][k-i];
        }
        printf("%d\n",dp[m][n]);
    }
    return 0;
}
时间: 2024-10-10 00:53:10

5 POJ 3132 Sum of Different Primes的相关文章

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

POJ 3132 Sum of Different Primes ( 满背包问题)

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3280   Accepted: 2040 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

题目链接: POJ:http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2822 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive int

poj 3132

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3360   Accepted: 2092 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science,

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

poj3132 Sum of Different Primes

Sum of Different Primes Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3293   Accepted: 2052 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integer

[伯努利数] poj 1707 Sum of powers

题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 735   Accepted: 354 Description A young schoolboy would like to calculate the sum for some fixed natural k and different

zoj 2358,poj 1775 Sum of Factorials(数学题)

题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n能否表示成几个数的阶乘之和 //这里有一个重要结论:n!>(0!+1!+……+(n-1)!), //证明很容易,当i<=n-1时,i!<=(n-1)!,故(0!+1!+……+(n-1)!)<=n*(n-1)!=n!. // 由于题目规定n<=1000000,而10!=362880