POJ3132 Sum of Different Primes
Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two positive integers n and k, you should count the number of ways to express n as a sum of k different primes. Here, two ways are considered to be the same if they sum up the same set of the primes. For example, 8 can be expressed as 3 + 5 and 5 + 3 but the are not distinguished. When n and k are 24 and 3 respectively, the answer is two because there are two sets {2, 3, 19} and {2, 5, 17} whose sums are equal to 24. There are not other sets of three primes that sum up to 24. For n = 24 and k = 2, the answer is three, because there are three sets {5, 19}, {7, 17} and {11, 13}. For n = 2 and k = 1, the answer is one, because there is only one set {2} whose sum is 2. For n = 1 and k = 1, the answer is zero. As 1 is not a prime, you shouldn’t count {1}. For n = 4 and k = 2, the answer is zero, because there are no sets of two different primes whose sums are 4. Your job is to write a program that reports the number of such ways for the given n and k. Input The input is a sequence of datasets followed by a line containing two zeros separated by a space. A dataset is a line containing two positive integers n and k separated by a space. You may assume that n ≤ 1120 and k ≤ 14. Output The output should be composed of lines, each corresponding to an input dataset. An output line should contain one non-negative integer indicating the number of the ways for n and k specified in the corresponding dataset. You may assume that it is less than 231. Sample Input 24 3 Sample Output 2 Source |
用素数筛打一个素数表出来,然后在素数表上背包动规。
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int mxn=;
int pri[mxn],cnt=;
int vis[mxn];
int n,k;
int f[mxn][];
void Pri(){
int i,j;
for(i=;i<=n;i++){
if(!vis[i]){
pri[++cnt]=i;
}
for(j=;j<=cnt && i*pri[j]<=n;j++){
vis[i*pri[j]]=;
if(i%pri[j]==)break;
}
}
return;
}
int main(){
n=;
Pri();
int i,j;
f[][]=;
for(i=;i<=cnt;i++){
for(j=n;j>=pri[i];j--){
for(int k=;k<=;k++)
f[j][k]+=f[j-pri[i]][k-];
}
}
while(scanf("%d%d",&n,&k) && n){
printf("%d\n",f[n][k]);
}
return ;
}
POJ3132 Sum of Different Primes的更多相关文章
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...
- sicily 1259. Sum of Consecutive Primes
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- [UVa1213]Sum of Different Primes(递推,01背包)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 1213 (01背包变形) Sum of Different Primes
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...
- zoj 2822 Sum of Different Primes (01背包)
///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # in ...
- UVA 1213 Sum of Different Primes(经典dp)
题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 ...
- UVA 1213 Sum of Different Primes
https://vjudge.net/problem/UVA-1213 dp[i][j][k] 前i个质数里选j个和为k的方案数 枚举第i个选不选转移 #include<cstdio> # ...
- UVA 1213 - Sum of Different Primes(递推)
类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. ...
- UVa 1213 Sum of Different Primes (DP)
题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法. 析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了. 代码如下: #pragma ...
随机推荐
- VirtualBox Network设置的NAT和Bridged Adapter模式区别
区别: NAT模式下,虚拟机仍然可以访问网络,但是从网络接收者的眼中看来,这些网络请求都来自宿主机,而感知不到虚拟机.外网也无法访问虚拟机网络.虚拟机和宿主机器的IP地址在不同的子网,比如192.16 ...
- 6.3 lambda 表达式
6.3.1 lambda 表达式是一个可传递的代码块,可以在以后执行一次或者多次. 思考(如何按指定时间间隔完成工作,将这个工作放在一个ActionListener的actionPerformed方法 ...
- 51nod 1412 AVL数的种类(DP
题意给了n个节点 问AVL树的种类 卧槽 真的好傻 又忘记这种题可以打表了 就算n^3 也可以接受的 树的深度不大 那么转移方程很明显了 dp[i][j] 代表的是节点为n深度为j的树的种类 k ...
- leetcode_1095. Find in Mountain Array_[Binary Search]
https://leetcode.com/problems/find-in-mountain-array/ 题意:给定一个MountainArray(定义见题目),找到其中最早出现的target值的下 ...
- ubuntu 18.* 重启网卡
systemctl UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting A ...
- POJ-3050-Hoscotch
这是一道简单的深搜题目,题意说的是给一个5*5的棋盘,里面填满数字,然后跳到一个格子上,这是第一步,接着向上下左右四个方向任意一个方向走一步,一共走6步,问我们走过的数字组成的一个6位数有多少种不同的 ...
- 几种优化web页面加载速度的策略
剥离静态资源请求到CDN 一般在主域名下的HTTP请求里都会携带大量Cookie信息,最大4KB,每个域名下最多50条:但如果仅仅访问js/css/jpeg等静态资源文件的话是不需要Cookie信息, ...
- 继上次编译openwrt之后,添加web界面
上编博客写了关于openwrt编译环境和编译一个默认配置的openwrt系统. 现在我正在做如何添加web界面.(hiwooya自带的luci web) 方法如下: 首先在编译环境中配置 make m ...
- 【git】自动换行转换autocrlf
#####windows git config --global core.autocrlf true #####linux git config --global core.autocrlf inp ...
- python中实现格式化输出 %用法
当我们在python中需要打印出特定格式的内容时可以用到这个方法,方法介绍如下: 例如我们现在要收集用户的一些个人信息,这时候我们的代码如下: name=input("name: " ...