hdu 4345 Permutation 记忆化搜索

思路:实际上求的是和小于等于n的质数的种类数!!!

代码如下:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 50000
using namespace std;
int prime[],cnt;
ll dp[][];
bool f[];
void init()
{
cnt=;
for(int i=;i<=;i++){
if(!f[i]) prime[cnt++]=i;
for(int j=;j<cnt&&i*prime[j]<=;j++){
f[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
}
ll dfs(int n,int m)
{
if(dp[n][m]!=-) return dp[n][m];
if(prime[m]>n) return dp[n][m]=;
ll ans=;
int k=;
while(k<=n){
ans+=dfs(n-k,m+);
if(!k) k=prime[m];
else k*=prime[m];
}
return dp[n][m]=ans;
}
int main(){
init();
memset(dp,-,sizeof(dp));
int n;
while(scanf("%d",&n)!=EOF){
printf("%I64d\n",dfs(n,));
}
return ;
}
上一篇:HDU 1429 (BFS+记忆化状压搜索)


下一篇:串口调试,提示the given port name does not start with COM/com异常解决办法,,发现是打印机在搞怪