Number DP backpack embodiment --- P1832 A + B Problem (upgrade)

P1832 A + B Problem (upgrade)

Description face questions

Given a positive integer n, to find the total number of programs and broken down into a number of prime numbers.

answer

We can consider backpack DP achieve

Number of DP board backpack program title

f[ i ] = f[ i ] + f[ i - a[j] ] 

 

F [j] represents the total number scheme represented by a plurality of j primes

 

note

1. Do not linear sieve wrong:

  1)not_prime[maxn] maxn>=n

  After 2) memset not_prime array, 0,1 initialization is not prime

 2. Program Number array to open long long DP

 

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<queue>

using namespace std;

typedef long long ll;

inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

int n;
int prime[1000],not_prime[1050],cnt=0;
ll f[5000];

void xxs()
{
    memset(prime,0,sizeof(prime));
    memset(not_prime,0,sizeof(not_prime));
    not_prime[0]=not_prime[1]=1;
    for(int i=2;i<=n;i++){
        if(!not_prime[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt;j++){
            if(i*prime[j]>n) break;
            not_prime[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}

int main ()
{
    n=read();
    xxs();
    f[0]=1;
    for(int i=1;i<=cnt;i++)
      for(int j=prime[i];j<=n;j++)
         f[j]+=f[j-prime[i]];
    printf("%lld\n",f[n]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/11972112.html