首页 技术 正文
技术 2022年11月21日
0 收藏 955 点赞 3,030 浏览 1807 个字

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

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893