UVA 1213 Sum of Different Primes
来源:互联网 发布:python array append 编辑:程序博客网 时间:2024/05/16 05:50
题意:求一个数n由k个不同的素数组成的方案数
还是dp 首先需要把素数筛出来
然后要注意一点dp的顺序 每个素数只能用一次 所以要把每个素数作为第一维 枚举第i个素数是否选取 来更新
dp[j][k] 中j表示已经选取的素数个数 k表示这j个数的总和 dp存的是方案数
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cctype>#include<cmath>#include<vector>#include<queue>#include<map>#include<hash_map>#include<algorithm>#include<set>#define scnaf scanf#define cahr char#define bug puts("bugbugbug");using namespace std;typedef long long ll;const int mod=10007;const int maxn=1e4+50;const int maxx=1e4;const int inf=1e9;//几乎线性的速度const int MAXP=1e5+100;bool is_prime[MAXP];int prime[MAXP/5],np;void init_prim(int n){ memset(is_prime, true, sizeof(is_prime)); np = 0; for (int i = 2; i <= n; i++){ if (is_prime[i]) prime[np++] = i; for (int j = 0; ((j < np) && (i * prime[j] <= n)); ++j){ is_prime[i * prime[j]] = 0; if (i % prime[j] == 0) break; //点睛之笔 } }}ll dp[16][1125]={0};void init(){ init_prim(1120); dp[0][0]=1; for(int i=0;i<np;i++) for(int j=13;j>=0;j--) for(int k=0;k+prime[i]<=1120;k++) dp[j+1][k+prime[i]]+=dp[j][k];}int main(){ init(); int n,k; while(~scanf("%d%d",&n,&k)&&(n||k)) printf("%lld\n",dp[k][n]); return 0;}
0 0
- UVA 1213 Sum of Different Primes
- UVA 1213 Sum of Different Primes
- UVA 1213(p343)----Sum of Different Primes
- uva 1213 Sum of Different Primes, dp
- Sum of Different Primes UVA
- Sum of Different Primes UVA
- uva 1213 sum of different primes 01背包(二维)
- Sum of Different Primes
- 1213 - Sum of Different Primes(DP)
- UVa1213 Sum of Different Primes
- poj3132 Sum of Different Primes
- poj3132 Sum of Different Primes
- WOJ1274-Sum of Different Primes
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)
- POJ 3132 Sum of Different Primes
- Poj 3132 Sum of Different Primes
- POJ3122 - Sum of Different Primes - 动态规划
- DP Sum of Different Primes POJ 3132
- HDU 3398 String(整数唯一分解定理)
- HDU 1827
- 类加载器
- Web前端编码规范
- 常用正则表达式
- UVA 1213 Sum of Different Primes
- 虚拟继承和多重继承中类对象的大小
- CF#307-E. GukiZ and GukiZiana-分块法/平方分桶
- 超时函数装饰器
- linux环境下的code
- Git and GitHub
- 使用缓冲区修改复制文本文件
- 第一篇博客,当然是先给我心爱的Android
- Android沉淀学习1-android studio部分快捷键