洛谷 P1832 A+B Problem(再升级)(DP)

https://www.luogu.com.cn/problem/P1832

题目大意:给定一个数字n,求它可以由素数构成的次数?

eg:7
7=7;
7=2+5;
7=2+2+3;

所以答案输出3

一定要开longlong啊~

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL N=200200,M=2002;
const int INF=0x3f3f3f3f;
LL f[N];
LL primes[N],cnt=0;
bool st[N];
void get_primes(LL n)//欧拉筛
{
    for(LL i=2;i<=n;i++)
    {
        if(!st[i]) primes[cnt++]=i;
        for(LL j=0;primes[j]<=n/i;j++)
        {
            st[primes[j]*i]=true;
            if(i%primes[j]==0) break;
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        get_primes(1000);
        LL x;
        cin>>x;
        //完全背包
        f[0]=1;
        for(LL i=2;i<=x;i++)
        {
            if(!st[i])//必须得是质数才能够参与运算
            {
                for(LL j=i;j<=x;j++)//从当前质数开始构造
                {
                    f[j]+=f[j-i];
                    //cout<<i<<" "<<j<<" "<<f[j]<<endl;
                }
            }
        }
        cout<<f[x]<<endl;
    }
    return 0;
}
posted @ 2022-09-05 19:55  高尔赛凡尔娟  阅读(28)  评论(0编辑  收藏  举报