Sgu 116
|
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 |
||||||
|
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <queue> 6 #include <set> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 #define INF 0x7fffffff 12 #define mod 1000000007 13 #define ll long long 14 #define maxn 20025 15 #define pi acos(-1.0) 16 int n, m, k, c, ans=10000; 17 int prv[maxn], pri[maxn],pr2[maxn]; 18 int dp[maxn],p[maxn]; 19 bool cmp(int a, int b){ return a > b; } 20 int main(){ 21 for (int i = 2; i <= 10000;i++) 22 if (!prv[i])for (int j = i*i; j <= 10000; j += i) 23 if (!prv[j])prv[j] = 1; 24 for (int j = 0; j < maxn; j++)dp[j] = 10000; 25 for (int i = 2; i <= 10000; i++)if (!prv[i])pri[k++] = i; 26 for (int i = 2; i < k; i++)if (!prv[i])pr2[c++] = pri[i-1]; 27 scanf("%d", &n); 28 dp[0] = 0; 29 for (int i = 0; i < c;i++) 30 for (int j = pr2[i]; j <= 10000; j++) 31 dp[j] = min(dp[j], dp[j - pr2[i]] + 1); 32 if (dp[n] >= 10000)dp[n] = 0; 33 printf("%d\n", dp[n]); 34 int t=n,x = 0; 35 for (int i = dp[n]-1; i >=0; i--) 36 for (int j = 0; j < c; j++) 37 if (dp[t - pr2[j]]==i){ 38 p[x++] = pr2[j]; 39 t -= pr2[j]; 40 break; 41 } 42 sort(p, p+x, cmp); 43 for (int i = 0; i < x; i++)printf("%d ", p[i]); 44 return 0; 45 }
浙公网安备 33010602011771号