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 ...
随机推荐
- python一周速成学习笔记
目录 一:语法元素 1.注释,变量,空格的使用 2.输入函数,输出函数 3.分支语句,循环语句 4.保留字in,同步赋值 5.import与def以及turtle库 6.eval函数与repr函数 二 ...
- Nengo 神经网络
Nengo被加拿大滑铁卢大学的神经学家和软件工程师表示,这是迄今为止产生的世界上最复杂.最大规模的人类大脑模型模拟.这个名叫Spaun的大脑由250万 个模拟神经元组成,它能执行8种不同类型的任务.这 ...
- ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, url: jdbc:mysql://localhost:3306/xhb?useUnicode=true&characterEncoding=UTF-8, errorCode 1045, sta
ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, ...
- WPF知识点全攻略05- XAML内容控件
此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...
- Java Miniui实现批量上传文件demo 201906221520
可能需要的jar包: 需要miniui(类似easyui). Test2019062201.jsp <%@ page language="java" contentType= ...
- java在线聊天项目1.3版 ——设计好友列表框功能
设计好友列表框功能,思路—— 1.当客户端成功登陆后,则客户端把成功登陆信息发送给服务端, 2.由服务端将接收到来自各个成功登陆的客户端的用户信息添加进好友列表, 3.每当有成功登陆的用户就向各个客户 ...
- Mysql的慢日志
一.开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二.慢日志参数: slow_query_log 慢查询开启状态slow_qu ...
- windows2012安装sql2012报错
一. 处理
- Python列表,元组,字典,集合详细操作
菜鸟学Python第五天 数据类型常用操作及内置方法 列表(list) ======================================基本使用====================== ...
- leetcode-1-basic
leetcode-algorithm 1. Two Sum 解法:循环,试呗..简单粗暴.. class Solution { public: vector<int> twoSum(vec ...