SGU 106.Index of super-prime
时间限制:0.25s
空间限制:4M
题目大意:
在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000),求最少能用几个超级素数的和表示,并以降序输出这些超级素数。
Sample Input
6
Sample Output
2
3 3
{=============}
分析:
读入n以后,先将不大于n的Super-prime筛出,然后DP
简单点的直接用完全背包DP,
稍微优化一点,减少一半的循环次数的话
用 F[i]=max(f[i],f[j]+f[i-j]);
其它细节要动手做了才知道。
参考代码:
#include <iostream>
#include <cstring>
using namespace std;
int prime[11000], pd[11000], tol, sPrime[1000], sum;
int f[11000][3], n;
void init() {
for (int i = 2; i <= n; i++) {
if (!pd[i]) {
prime[++tol] = i;
for (int j = i + i; j <= n; j += i) pd[j] = 1;
}
}
for (int i = 2; i <= tol; i++)
if (!pd[i]) sPrime[++sum] = prime[i];
memset (pd, 0, sizeof pd);
for (int i = 1; i <= sum; i++)
pd[sPrime[i]] = 1;
}
void write (int x) {
if (f[x][0] == 1) cout << x << ' ';
else
write (f[x][2]), write (f[x][1]);
}
int main() {
cin >> n;
init();
for (int i = 3; i <= n; i++) {
if (pd[i]) f[i][0] = 1;
else {
for (int j = 3; j <= i / 2 + 1; j++){
if (f[j][0] && f[i - j][0] &&
(f[i][0] == 0 || f[i][0] > f[j][0] + f[i - j][0])){
f[i][0] = f[j][0] + f[i - j][0];
f[i][1] = j, f[i][2] = i - j;
}
}
}
}
cout << f[n][0] << endl;
if (f[n][0]) write (n);
return 0;
}
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处
SGU 106.Index of super-prime的更多相关文章
- SGU 106 The equation
H - The equation Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Subm ...
- 扩展欧几里德 SGU 106
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=106 题意:求ax + by + c = 0在[x1, x2], [y1, y2 ...
- 素数 + 背包 - SGU 116. Index of super-prime
Index of super-prime Problem's Link Mean: 如果一个素数所在的位置还是素数,那么这个素数就是超级素数,比如3在第2位置,那么3就是超级素数. 现在给你一个数,求 ...
- SGU 106 The equation 扩展欧几里德
106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...
- The equation SGU - 106
题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...
- SGU 106 The equation 扩展欧几里得好题
扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...
- The equation - SGU 106(扩展欧几里得)
题目大意:有一个二元一次方程,给出系数值和x与y的取值范围,求出来总共有多少对整数解. 分析:有以下几点情况. 1,系数a=0, b=0, 当c != 0的时候结果很明显是无解,当c=0的时候x,y可 ...
- SGU 106 The Equation 扩展欧几里得应用
Sol:线性不定方程+不等式求解 证明的去搜下别人的证明就好了...数学题. #include <algorithm> #include <cstdio> #include & ...
- sgu 116 Index of super-prime
题意:用最少的super-prime组成n; 找出所有的super-prime数,只有202个.用完全背包记录能取到n值的最少数量.再找出7要哪些元素. #include <iostream&g ...
随机推荐
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- 网络流(费用流)CodeForces 321B:Ciel and Duel
Fox Ciel is playing a card game with her friend Jiro. Jiro has n cards, each one has two attributes: ...
- ant 学习与开发
ant 学习与开发 http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html
- java基础(五)
这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...
- How to make apq8084
1.first of all ,you will meet many problem no excute permisson,pls do : chmod -R +x APQ8084/ 2. buil ...
- android进程间通信:使用AIDL
android 的binder其实是基于 openbinder实现的,openbinder的地址:http://www.angryredplanet.com/~hackbod/openbinder/d ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- C#添加资源的两种方式
1.粘贴到项目Properties中的Resources.resx中 base.m_bitmap = Properties.Resources.MeasuredisTool; 2.添加已有资源中的bm ...
- 1388 - Graveyard(数论)
题目链接:1388 - Graveyard 题目大意:在一个周长为10000的圆形水池旁有n个等距离的雕塑,现在要再添加m个雕塑,为了使得n + m个雕塑等距离,需要移动一些雕塑,问如何使得移动的总位 ...
- Linux命令 — 设置或查看网络配置命令ifconfig
ifconfig命令用于设置或查看网络配置,包括IP地址.网络掩码.广播地址等.它是linux系统中,使用频率最高的关于网络方面的命令. 1. 命令介绍 命令格式: ifconfig [interfa ...