UVa 1213 Sum of Different Primes (DP)
题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法。
析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define print(a) printf("%d\n", (a))
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1120 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn];
vector<int> prime;
int dp[maxn][15]; int main(){
memset(a, 0, sizeof(a));
m = (int)sqrt(maxn + 0.5);
for(int i = 2; i <= m; i++) if(!a[i])
for(int j = i * i; j < maxn; j += i) a[j] = 1;
for(int i = 2; i < maxn; ++i) if(!a[i]) prime.push_back(i);
memset(dp, 0, sizeof dp); dp[0][0] = 1;
for(int k = 0; k < prime.size(); ++k){
for(int i = 1120; i >= prime[k]; --i){
for(int j = 1; j <= 14; ++j)
dp[i][j] += dp[i-prime[k]][j-1];
}
} while(scanf("%d %d", &n, &m) == 2 && m+n) printf("%d\n", dp[n][m]);
return 0;
}
UVa 1213 Sum of Different Primes (DP)的更多相关文章
- 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 (不同素数之和)(dp)
题意:选择k个质数,使它们的和等于n,问有多少种方案. 分析:dp[i][j],选择j个质数,使它们的和等于i的方法数. #pragma comment(linker, "/STACK:10 ...
- 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]. ...
- 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 ...
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
- UVA - 825Walking on the Safe Side(dp)
id=19217">称号: UVA - 825Walking on the Safe Side(dp) 题目大意:给出一个n * m的矩阵.起点是1 * 1,终点是n * m.这个矩阵 ...
- UVa 1213 (01背包变形) Sum of Different Primes
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...
随机推荐
- Swift--错误集:Class controller has not initializers
bug错误图 解决方法: 如下图所示,visitor这个属性并没有拆包处理,及将UIViewController的子类中的变量全部进行拆包处理,就是在变量声明的时候加一个?号,在使用的时候拆包处理,加 ...
- Java重写equals方法和hashCode方法
package com.ddy; public class User { private Integer id; private String name; private St ...
- Java调用WSDL接口
1.首先准备jar包: 2.代码调用如下: String url="url地址"; QName qName=new QName("命名空间","接口名 ...
- datasnap使用ipv6
有些人说DATASNAP不支持IPv6,只支持IPv4. 这是不正确的. DATASNAP默认是使用IPv4在ipv6 环境下 怎样用datasnap?Params.Values['Communica ...
- [RxJS] Use `lift` to Connect a `source` to a `subscriber` in RxJS
The lift method on each source hides away the internals of RxJS so you can simply connect a source t ...
- 使用POI操作Excel时new XSSFWorkbook ()报错java.lang.NoSuchMethodError解决方式
使用最新的POI3.11时,在执行 Workbook workBook = new XSSFWorkbook ();这段代码时出现错误: java.lang.NoSuchMethodError: j ...
- poj 1258 Agri-Net(Prim)(基础)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44487 Accepted: 18173 Descri ...
- POST & GET & Ajax 全解
GET&POST&Ajax 全解 一.POST和GET的差别 GET:GET方法提交数据不安全,数据置于请求行.客户段地址栏可见:GET方法提交的数据限制大小在255个字符之内.參数直 ...
- ubuntu 安装后要做的事情
1. 安装chrome,软件中心就可以. 2. 安装vim 和一些插件.这里引入一大牛配置的插件集 sudo apt-get install vim-gtk wget -qO- https://raw ...
- [思考]我们应该怎样建设企业IT
从人员架构上来看,要不要企业自己的IT团队?如果要,应该有什么样的架构?运维,开发,管理,项目? 从是否外包角度看,要不要外包?如果外包,如何建立外包流程? 从业务角度看,企业IT的发展应该是围绕业务 ...