思路:实际上求的是和小于等于n的质数的种类数!!!
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<iomanip> 5 #include<cmath> 6 #include<cstring> 7 #include<vector> 8 #define ll __int64 9 #define pi acos(-1.0) 10 #define MAX 50000 11 using namespace std; 12 int prime[1105],cnt; 13 ll dp[1105][1100]; 14 bool f[1005]; 15 void init() 16 { 17 cnt=0; 18 for(int i=2;i<=1100;i++){ 19 if(!f[i]) prime[cnt++]=i; 20 for(int j=0;j<cnt&&i*prime[j]<=1100;j++){ 21 f[i*prime[j]]=1; 22 if(i%prime[j]==0) break; 23 } 24 } 25 } 26 ll dfs(int n,int m) 27 { 28 if(dp[n][m]!=-1) return dp[n][m]; 29 if(prime[m]>n) return dp[n][m]=1; 30 ll ans=0; 31 int k=0; 32 while(k<=n){ 33 ans+=dfs(n-k,m+1); 34 if(!k) k=prime[m]; 35 else k*=prime[m]; 36 } 37 return dp[n][m]=ans; 38 } 39 int main(){ 40 init(); 41 memset(dp,-1,sizeof(dp)); 42 int n; 43 while(scanf("%d",&n)!=EOF){ 44 printf("%I64d ",dfs(n,0)); 45 } 46 return 0; 47 }