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 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+树思想的更多相关文章

  1. 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Descrip ...

  2. 【Tsinsen A1039】【bzoj2638】黑白染色 (BFS树)

    Descroption 原题链接 你有一个\(n*m\)的矩形,一开始所有格子都是白色,然后给出一个目标状态的矩形,有的地方是白色,有的地方是黑色,你每次可以选择一个连通块(四连通块,且不要求颜色一样 ...

  3. AcWing:176. 装满的油箱(bfs + dijiskla思想)

    有N个城市(编号0.1…N-1)和M条道路,构成一张无向图. 在每个城市里边都有一个加油站,不同的加油站的单位油价不一样. 现在你需要回答不超过100个问题,在每个问题中,请计算出一架油箱容量为C的车 ...

  4. 快速切题 sgu113 Nearly prime numbers 难度:0

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

  5. “盛大游戏杯”第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 描述 在一个矩形的灰度图像上,每个 ...

  6. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

  7. 快速切题 sgu103. Traffic Lights 最短路 难度:1

    103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...

  8. 快速切题 poj3414 Pots

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10042   Accepted: 4221   Special J ...

  9. 快速切题 hdu2416 Treasure of the Chimp Island 搜索 解题报告

    Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. git分支 远程协作

    创建文件mkdir ### cd ### git init 初始化 git config global user.name “username” git config global user.emia ...

  2. python数据库编程小应用

    python DB api 数据库连接对象connection数据库交互对象cursor数据库异常类exceptions 流程:开始创建connection获取cursor执行查询.执行命令.获取数据 ...

  3. Python3基础 ** 幂运算 // 整除运算

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. Python3基础 str *运算 重复拼接字符串

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. lightoj 1341 Aladdin and the Flying Carpet(算术基本定理)题解

    题意:给一个矩形(非正方形)面积a和最小边长b,要求边长均大于b,求这样的矩形有几个 思路:先用到了之前学的质因数分解,还有一个新的公式: 然后我们可以先算出a的所有约数,因为只算约数个数面积重复,所 ...

  6. 导出数据库表为world文档说明,以及PowerDesigner导出表结构pdm设计文档

    如何使用“mysql导出数据库结构为world工具”以及如何使用powerdesigner映射数据库模型 一.通过powerdesigner配置ojdbc 1.安装并打开powerdesigner,新 ...

  7. 【问题解决】An internal error occurred during: "Computing additional info". Could not initialize class javax.crypto.JceSecurityManager

    在使用eclipse时对象后使用点操作符时总是会弹出错误,很是烦人 An internal error occurred during: "Computing additional info ...

  8. 【集群搭建】Zookeeper集群环境配置

    1.下载解压安装文件 2.配置文件:conf/zoo.cfg tickTime=2000 dataDir=/usr/sunny/logs/zookeeper/data dataLogDir=/usr/ ...

  9. 【jdk源码分析】java.lang.Appendable

    1.概述 public interface Appendable 能够被添加 char 序列和值的对象.如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appenda ...

  10. 如何加速tomcat启动速度

    在tomcat启动的时候,我们可以做一些优化设置来使得tomcat的启动更快速,下面是一些常见的优化加速启动的方法建议(以tomcat-7.+为例说明). 1.  Jars包 1.1.将一些不必要的j ...