2189 ACM 母函数 素数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2189

思路:先找出150以内的 素数,然后再用母函数或01背包计算

复习母函数的代码:https://www.cnblogs.com/CheeseIce/p/9595315.html

扩展:有看到一个博主写:n>5,后素数在6*N(N为正整数)的左右两边,左或右,所以可以很简单的把素数写出来

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int main(){
    int num[35]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149};
    int n,k,i,j,t;
    int c1[160],c2[160];
    cin>>t;
    while(t--){
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        cin>>n;
        for(i=0;i<=n;i+=2)
        {
            c1[i]=1;
        }
        for(i=1;i<35;i++)
        {
            for(j=0;j<=n;j++)
            {
                for(k=0;k+j<=n;k+=num[i])
                {
                    c2[j+k]+=c1[j];
                }
            }
            for(j=0;j<=n;j++)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        cout<<c1[n]<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/CheeseIce/p/9665353.html