UVA 1213 Sum of Different Primes】的更多相关文章

题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 但是注意三重循环的顺序与位置,我们要防重防漏 第一重循环是枚举每个小于n的素数,思路是对于每个素数放入dp里面的位置 第二重倒叙枚举每个数n,倒序是类似01背包不能让枚举的素数重复加入同一个dp数组中 第三重正序枚举个数k只能放在最里面,这样才不会出现重复 import java.util.Scanner;…
https://vjudge.net/problem/UVA-1213 dp[i][j][k] 前i个质数里选j个和为k的方案数 枚举第i个选不选转移 #include<cstdio> #define N 1121 using namespace std; ][][N]; bool v[N]; int main() { ;i<N;i++) { if(!v[i]) { v[i]=true; p[++cnt]=i; } ;j<=cnt;j++) { if(i*p[j]>=N) b…
类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. 但是题目还有一个要求是不同素数之和,为了保证素数不同,那就先枚举素数吧, f[i][n][k] = sigma  f[i-1][n-p][k-1], 然后滚动数组降掉一维就好了. 本地筛一遍发现maxn 范围内素数数量是pi_n = 187. maxn*maxk*pi_n ≍ 1e6. 复杂度也是没…
题意:给定两个数 n 和 k,问你用 k 个不同的质数组成 n,有多少方法. 析:dp[i][j] 表示 n 由 j 个不同的质数组成,然后先打表素数,然后就easy了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #…
题意:选择k个质数,使它们的和等于n,问有多少种方案. 分析:dp[i][j],选择j个质数,使它们的和等于i的方法数. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<i…
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2822 Description A positive integer may be expressed as a sum of different prime numbers (primes), in one way or another. Given two…
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = sigma d(i-1, j-p[k]) ,其中p[k]表示第k个素数 注意递推的顺序是倒着推的,否则会计算重复的情况. 代码中第二重和第三重循环的顺序可互换. #include <cstdio> #include <cmath> ; ; ; int prime[maxp]; ];…
Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3473   Accepted: 2154 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 co…
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3654 题意:把n拆成k个不同素数的和,有多少种拆法. dp(i,j)表示数字为i时,有j个不同素数和的组合数. 先枚举素数的上界k,注意是不同素数,那就再在k个素数中做01背包,dp(i,j)+=dp(i-p,j-1)统计出现次数就行了. #include <bits/st…
https://vjudge.net/problem/UVA-10891 题意: 有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取.每次玩家只能从左端或者右端取任意数量个数,但不能两端都取.所有数都被取走后游戏结束,然后统计每个人取走的所有数之和,作为各自的得分.两个人采取的策略都是让自己的得分尽量高,并且两个人都足够聪明,求A的得分减去B的得分后的结果. 思路: 不管是轮到谁取数,都是在一个序列中从左边或右边开始取最大值. 那么我们就令d[i][j]表示先手在[i~j]序列中所能取到…
Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53…
https://vjudge.net/problem/UVA-1210 统计质数前缀和,枚举左右端点,这一段的区间和+1 #include<cstdio> #define N 10001 using namespace std; int cnt,p[N],sum[N],ans[N]; bool v[N]; int main() { ;i<N;i++) { if(!v[i]) { v[i]=true; p[++cnt]=i; } ;j<=cnt;j++) { if(i*p[j]>…
Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive prim…
刚看到这个题目不知道怎么个DP法,有点难想到 解法如下 设置dp[i][j]代表i到j这段子序列能获得的最大值,这样,枚举m=min(m,dp[i+1到j][j],dp[i][i到j-1]),m就代表了给另一个人的,就可求得就只能取到 dp[i][j]-sum(i,j)-m,这样,sum为该段序列总和,只需简单的数列前缀和即可求得sum,这样,只需枚举m值,不断向下进行记忆化搜索,即可求得终结果. #include <iostream> #include <cstdio> #inc…
///给你n 求他能分解成多少个的不同的k个素数相加之和 ///01背包,素数打表 # include <stdio.h> # include <algorithm> # include <string.h> # include <math.h> # include <iostream> using namespace std; int cot; int used[1500]; int prime[1500]; void sushu()///素数…
题意: 求 ,要求M尽量小. 析:这其实就是一个伯努利数,伯努利数公式如下: 伯努利数满足条件B0 = 1,并且 也有 几乎就是本题,然后只要把 n 换成 n-1,然后后面就一样了,然后最后再加上一个即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inclu…
题目:给你一个数字问将他写成连续的数字的和的形式.有几种写法. 分析:数论. 设拆成的序列个数为k,我们分两种情况讨论: 1.拆成奇数个连续数.那么设中位数是a,则有n = k * a: 2.拆成偶数个连续数,那么设中位数是a与a+1,则有n = k / 2 *(a+a+1). 综上所述,本问题就是将n拆成2个数的乘积的形式,且当中一个一定为奇数: 问题转化成求解n中奇数因子的个数.这里求出全部的奇素因子计算组合数就可以. 说明:实际的数据规模没有题面上那么大╮(╯▽╰)╭. #include…
这里就是01背包多了一维物品个数罢了 记得不能重复所以有一层循环顺序要倒着来 边界f[0][0] = 1 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) for(int i = (a); i < (b); i++) using namespace std; const int MAXN = 1121; bool is_prime[MAXN]; vector<in…
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 译文: 10以下的素数之和为17,求出2000000以下的素数之和. ======================= 第一次code: import java.util.Scanner; public class Main { public static void main(String[]…
http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description All you know Goldbach conjecture.That is to say, Every even integer great…
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3339    Accepted Submission(s): 953 Problem Description All you know Goldbach conjecture.That is to say, Every even int…
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conje…
Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr…
是我算法不对,还是笔记本CPU太差? 我优化了两次,还是花了三四个小时来得到结果. 在输出上加1就是最终结果. The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. def isprime(n): boolisprime = True for i in xrange(3,n): if n % i == 0: boolisprime = Fals…
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 860 Accepted Submission(s): 278 Problem Description All you know Goldbach conjecture.That is to say, Every even integer gr…
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. #include<stdio.h> #include<math.h> #include<stdbool.h> #define N 2000000 bool prim(int n) { int i; ; i*i<=n; i++) { ) return false…
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 150 Problem Description All you know Goldbach conjecture.That is to say, Every even inte…
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10168.html 原创:Summation of Four Primes - PC110705 作者:MilkCu 题目描述 Summation of Four Primes   Waring's prime number conjecture states that every odd integer is either pri…
Difference Between Primes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 832    Accepted Submission(s): 267 Problem Description All you know Goldbach conjecture.That is to say, Every even inte…
title: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 翻译: 10下面的质数的和为2 + 3 + 5 + 7 = 17. 请求出200,0000下面全部质数的和. import math,time def isOk(a): for i in range(2,int(math.sqrt(a))+1): if a%i==0: retu…