题意:

选择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的更多相关文章

  1. 紫书 例题 9-5 UVa 12563 ( 01背包变形)

    总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...

  2. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  3. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  4. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  5. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  7. 紫书 习题 10-5 UVa 1213(01背包变形)

    这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> # ...

  8. 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 ...

  9. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

随机推荐

  1. WPF 概述

    WPF 全称是:Windows Presentation Foundation,直译为Windows表示基础.WPF是专门为GUI(Graphic User Interface)程序开发设计的. 在过 ...

  2. 1018. Public Bike Management (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...

  3. drop column与set unused

    8i以前,如果需要删除表中的列,需要删除表然后重新建.现在,但我们需要删除一个列时,可以有以下两种方法: Logical Delete Physical Delete Logical Delete(逻 ...

  4. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...

  5. html 设置Select options值进行绑定

    <select id="cdms"> <option value="">请选择...</option> <option ...

  6. csu 1312 榜单(模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec  Memory Limit: 128 ...

  7. phpd读取txt文件(自动解析换行)

    <form id="form" method="post" action="whois.php"> <?php $newf ...

  8. 《Head First HTML&CSS》笔记

    void元素是指HTML页面中开始标记和结束标记之间没有任何内容的元素. 应当使用相对链接来链接同一网站中的页面,而用URL来链接其他网站上的页面. 浏览器读取计算机本地文件时会使用file协议.文件 ...

  9. 2338: [HNOI2011]数矩形 - BZOJ

    因为已经看了一眼题解,知道是算中点和长度,相同时构成一个矩形,所以就把所有的线段算出来,然后排序,相同的就更新答案 为了避免误差,我们都用整数存,中点直接相加就行了,没必要除2,长度也只要平方就行了, ...

  10. c++ 字符串工具类

    #include <string> #include "util.h" namespace strtool{ std::string trim(const std::s ...