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;
}

  

最新文章

  1. 怎么在GitHub上寻找开源项目呢
  2. iOS 设置tableview头部不可以向下拉动..
  3. 51nod-1537 1537 分解(矩阵快速幂+找规律)
  4. 记录下 QT Linux 静态编译遇到的坑
  5. 使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境
  6. Map 排序
  7. KEIL C51 中嵌入汇编以及C51与A51间的相互调用
  8. Javascript:字符串分割split()妙用
  9. URL参数中有 特殊符号或加密数据 的问题解决
  10. Fizz-Buzz-Whizz
  11. Redis系列四(keepalived+lvs搭建负载均衡)
  12. 2019.01.10 bzoj1095: [ZJOI2007]Hide 捉迷藏(动态点分治)
  13. Mac 如何显示隐藏文件夹并设置快捷键
  14. js中如何访问对象和数组
  15. hdu1180 优先队列bfs+判断方向
  16. lerna基本试用
  17. Python中ndarray数组切片问题a[-n -x:-y]
  18. tcp 三次握手四次挥手
  19. bzoj3957
  20. ssl_protocols和ssl_ciphers应该怎么配置

热门文章

  1. NOIP 华容道
  2. 维特比算法Python实现
  3. weka中算法说明[转]
  4. ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn&#39;t want to study(树状数组)题解
  5. Unity3D学习笔记(四):物理系统碰撞和预制体
  6. 【Python】【内置函数】【bytes&amp;bytearray&amp;str&amp;array】
  7. React Native 之 定义的组件 (跨文件使用)
  8. WPF基础学习笔记整理 (四) 布局
  9. MongoDB(课时27 消除重复数据)
  10. RabbitMQ入门_13_消息持久化