forked from volzkzg/sgu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
116.cpp
63 lines (61 loc) · 1.18 KB
/
116.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
*NAME:Index of super-prime
*LANG:C++
*Source:sgu116
*/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
bool check[10101];
int prime[8000];
int n,k;
struct node{
bool f;
int pre;
int time;
}num[10101];
void print(int w){
if (w==0) return;
else print(w-num[w].pre);
cout << num[w].pre;
if (w!=k) cout << " " ;
else cout << endl;
}
int main(){
//init
num[0].f=true;
cin >> k;
for (int i=1;i<=k;++i) {num[i].f=false;num[i].time=0x7FFFFFFF/2;}
memset(check,true,sizeof(check));check[1]=false;
for (int i=2;i<=k;++i){
if (check[i]) prime[n++]=i;
for (int j=0;j<n;++j){
if (prime[j]*i>10000) break;
check[prime[j]*i]=false;
if (i%prime[j]==0) break;
}
}
//solve
for (int i=1;i<=k;++i)
for (int j=0;j<n;++j)
if (check[j+1] && i-prime[j]>=0){
if (num[i-prime[j]].f &&
num[i-prime[j]].time+1<num[i].time){
num[i].f=true;
num[i].pre=prime[j];
num[i].time=num[i-prime[j]].time+1;
}
}
//print
if (num[k].f){
cout << num[k].time << endl;
print(k);
}
else {
cout << 0 << endl;
}
return 0;
}