快速切题 sgu116. Index of super-prime bfs+树思想
116. Index of super-prime
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
Let P1, P2, … ,PN, … be a sequence of prime numbers. Super-prime number is such a prime number that its current number in prime numbers sequence is a prime number too. For example, 3 is a super-prime number, but 7 is not. Index of super-prime for number is 0 iff it is impossible to present it as a sum of few (maybe one) super-prime numbers, and if such presentation exists, index is equal to minimal number of items in such presentation. Your task is to find index of super-prime for given numbers and find optimal presentation as a sum of super-primes.
Input
There is a positive integer number in input. Number is not more than 10000.
Output
Write index I for given number as the first number in line. Write I super-primes numbers that are items in optimal presentation for given number. Write these I numbers in order of non-increasing.
Sample Input
6
Sample Output
2
3 3
用时:14min
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxnum=10001;
bool isntprime[maxnum];//是否质数
bool supp[maxnum];//是否superprime
int sup[300],pind;//把superprime放在一起
int ans[maxnum][2];//每个数必然=一个放在queue里的数+一个superprime,相当于记录路径
int vis[maxnum];//bfs记录状态
int n;
queue<int>que;
int bfs(){//找出当前数由那些超级质数构成,可以
while(!que.empty()&&vis[n]==0){
int tp=que.front();que.pop();
for(int i=0;i<pind;i++){
if(tp+sup[i]<=n&&vis[tp+sup[i]]==0){
vis[tp+sup[i]]=vis[tp]+1;
ans[tp+sup[i]][0]=sup[i];
ans[tp+sup[i]][1]=tp;
que.push(tp+sup[i]);
if(tp+sup[i]==n)break;
}
else if(tp+sup[i]>n)break;
}
}
return vis[n];
} void calc(){//找出超级质数
int cnt=1;
for(int i=3;i<maxnum;i+=2){
if(!isntprime[i]){
cnt++;
if(((cnt&1)!=0||cnt==2)&&!isntprime[cnt]){
sup[pind++]=i;
supp[i]=true;
vis[i]=1;
if(i<=n)que.push(i);
}
for(int j=3*i;j<maxnum;j+=2*i){
isntprime[j]=true;
}
}
}
} int main(){
scanf("%d",&n);
calc();
int num=bfs();
if(num==0){
puts("0");
}
else {
printf("%d\n",vis[n]);
int tn=n;
while(!supp[tn]){
printf("%d ",ans[tn][0]);
tn=ans[tn][1];
}
printf("%d\n",tn);
}
return 0;
}
快速切题 sgu116. Index of super-prime bfs+树思想的更多相关文章
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- 【Tsinsen A1039】【bzoj2638】黑白染色 (BFS树)
Descroption 原题链接 你有一个\(n*m\)的矩形,一开始所有格子都是白色,然后给出一个目标状态的矩形,有的地方是白色,有的地方是黑色,你每次可以选择一个连通块(四连通块,且不要求颜色一样 ...
- AcWing:176. 装满的油箱(bfs + dijiskla思想)
有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...
- 快速切题 sgu113 Nearly prime numbers 难度:0
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】
黑白图像直方图 发布时间: 2017年7月9日 18:30 最后更新: 2017年7月10日 21:08 时间限制: 1000ms 内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...
- 快速切题 sgu117. Counting 分解质因数
117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...
- 快速切题 sgu103. Traffic Lights 最短路 难度:1
103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...
- 快速切题 poj3414 Pots
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10042 Accepted: 4221 Special J ...
- 快速切题 hdu2416 Treasure of the Chimp Island 搜索 解题报告
Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- Python的递归
递归 是指函数/过程/子程序在运行过程序中直接或间接调用自身而产生的重入现象.在计算机编程里,递归指的是一个过程:函数不断引用自身,直到引用的对象已知.使用递归解决问题,思路清晰,代码少.但是在主流高 ...
- uboot启动提示Error, wrong i2c adapter 0 max 0 possible后卡住了怎么办
答:这个与i2c驱动有关,应该使能i2c驱动相关的配置项
- bzoj1055 ||P4290 [HAOI2008]玩具取名
目录 链接 思路 代码 链接 luogu bzoj 思路 区间dp 然后\(f[i][j][k]\) 区间\([i,j]\)能否变成\(k\)字符 (字符当然得转化一下) 字符的个数不多,直接暴力\( ...
- C#面试题集锦
1.貌似最常用的是 值类型和引用类型,还有 装箱和 http://www.cnblogs.com/Autumoon/archive/2008/08/18/1270685.html http://ww ...
- [不屈的复习] - 编辑工具IDE选取与Hello World
大家真正在工作中开发 java 应用都会使用eclipse,myeclipse, IntelliJ IDEA等等 现在还有vscode也支持了java扩展 在控制台下运行第一个Java程序,可以看到输 ...
- TensorFlow上实践基于自编码的One Class Learning
“我不知道什么是爱,但我知道什么是不爱” --One Class Learning的自白 一.单分类简介 如果将分类算法进行划分,根据类别个数的不同可以分为单分类.二分类.多分类,常见的分类算法主要解 ...
- C#高级编程第10版 note
泛型接口的抗变和协变 https://www.cnblogs.com/yanfang/p/6635302.html ①泛型接口,如果泛型类型前没有关键字out或者in来标注,则该泛型接口不支持抗变和协 ...
- 推荐一个SAM文件中flag含义解释工具--转载
SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...
- 词云wordcloud类介绍&python制作词云图&词云图乱码问题等小坑
词云图,大家一定见过,大数据时代大家经常见,我们今天就来用python的第三方库wordcloud,来制作一个大数据词云图,同时会降到这个过程中遇到的各种坑, 举个例子,下面是我从自己的微信上抓的微信 ...
- 浅谈PHP5中垃圾回收算法
原文链接:http://www.cnblogs.com/leoo2sk/archive/2011/02/27/php-gc.html PHP是一门托管型语言,在PHP编程中程序员不需要手工处理内存资源 ...