UVa 1213 (01背包变形) Sum of Different Primes
题意:
选择K个质数使它们的和为N,求总的方案数。
分析:
虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧。
d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = sigma d(i-1, j-p[k]) ,其中p[k]表示第k个素数
注意递推的顺序是倒着推的,否则会计算重复的情况。
代码中第二重和第三重循环的顺序可互换。
#include <cstdio>
#include <cmath> const int maxp = ;
const int maxn = ;
int cnt = ;
int prime[maxp];
bool vis[maxn + ]; int d[][maxn + ]; void Init()
{
int m = sqrt(maxn + 0.5);
for(int i = ; i <= m; ++i) if(!vis[i])
for(int j = i * i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; ++i) if(!vis[i]) prime[cnt++] = i;
} void dp()
{
d[][] = ;
for(int i = ; i < cnt; ++i)
for(int j = ; j > ; --j) //j个质数的和
for(int k = maxn; k >= prime[i]; --k) //为k
d[j][k] += d[j-][k-prime[i]];
} int main()
{
//freopen("in.txt", "r", stdin);
Init();
dp();
int k, n;
while(scanf("%d%d", &n, &k) == && n) printf("%d\n", d[k][n]); return ;
}
代码君
UVa 1213 (01背包变形) Sum of Different Primes的更多相关文章
- 紫书 例题 9-5 UVa 12563 ( 01背包变形)
总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...
- 【01背包变形】Robberies HDU 2955
http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- 紫书 习题 10-5 UVa 1213(01背包变形)
这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- POJ 3211 Washing Cloths(01背包变形)
Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...
随机推荐
- ShowMask
<html> <head> <script type="text/javascript"> function showMask(){ var a ...
- corsproxy
安装完 node.js运行 npm install -g corsproxy安装完成 corsproxy
- git - 必备指令
1. 查看远程分支 加上-a参数可以查看远程分支,远程分支会用红色表示出来(如果你开了颜色支持的话): $ git branch -a master remote tungway v1. * zron ...
- maven 编译部署src/main/java下的资源文件
maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...
- bzoj 1228: [SDOI2009]E&D 阿达马矩阵
1228: [SDOI2009]E&D Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 448 Solved: 240[Submit][Sta ...
- python读取mnist
python读取mnist 其实就是python怎么读取binnary file mnist的结构如下,选取train-images TRAINING SET IMAGE FILE (train-im ...
- javascript closure
http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...
- 学习笔记:shared_ptr陷阱
条款1:不要把一个原生指针给多个shared_ptr管理 int* ptr = new int; shared_ptr<int> p1(ptr); shared_ptr<int> ...
- 一个UUID生成算法的C语言实现 --- WIN32版本 .
一个UUID生成算法的C语言实现——WIN32版本 cheungmine 2007-9-16 根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...
- java,<E>什么意思?
泛型 就是指任意类型 比如 HashMap<K,V> 你用的时候 你可以将K,V 设置成任意类 HashMap<String,Integer> K,V 和那个E 一样 Hash ...