Uva 10168 - Summation of Four Primes
2014-07-26 21:51
381 查看
URL: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1109
Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive primes? I don’t know the answer. May be you can help!!! I want your solution
to be very efficient as I have a 386 machine at home. But the time limit specified above is for a Pentium III 800 machine. The definition of prime number for this problem is “A prime number is a positive number which has exactly two distinct integer factors”.
As for example 37 is prime as it has exactly two distinct integer factors 37 and 1.
Input
The input contains one integer number N (N<=10000000) in every line. This is the number you will have to express as a summation of four primes. Input is terminated by end of file.
Output
For each line of input there is one line of output, which contains four prime numbers according to the given condition. If the number cannot be expressed as a summation of four prime numbers print the line “Impossible.” in
a single line. There can be multiple solutions. Any good solution will be accepted.
Sample Input:
24
36
46
Sample Output:
3 11 3 7
3 7 13 13
11 11 17 7
Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive primes? I don’t know the answer. May be you can help!!! I want your solution
to be very efficient as I have a 386 machine at home. But the time limit specified above is for a Pentium III 800 machine. The definition of prime number for this problem is “A prime number is a positive number which has exactly two distinct integer factors”.
As for example 37 is prime as it has exactly two distinct integer factors 37 and 1.
Input
The input contains one integer number N (N<=10000000) in every line. This is the number you will have to express as a summation of four primes. Input is terminated by end of file.
Output
For each line of input there is one line of output, which contains four prime numbers according to the given condition. If the number cannot be expressed as a summation of four prime numbers print the line “Impossible.” in
a single line. There can be multiple solutions. Any good solution will be accepted.
Sample Input:
24
36
46
Sample Output:
3 11 3 7
3 7 13 13
11 11 17 7
#include <cstdio> #include <cstring> bool u[10000010]; int su[5000010]; int main(){ int i, j,n, num = 0; memset(u, true, sizeof(u)); for(i = 2; i < 10000000; i++){ if(u[i]) su[num++] = i; for(j = 0; j < num; j++){ if( i * su[j] > 10000000) break; u[ i*su[j] ] = false; if(i % su[j] == 0) break; } } while(~scanf("%d", &n) && n){ if(n < 8) {printf("Impossible.\n"); continue;} if(n % 2 == 0){printf("2 2 "); n -= 4;} else {printf("2 3 "); n -= 5;} for(i = 0; i < num; i++) if(u[ n-su[i] ]){ printf("%d %d\n", su[i], n - su[i]); break; } } return 0; }
相关文章推荐
- UVa Problem Solution: 10168 - Summation of Four Primes
- UVa 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVa 10168 Summation of Four Primes
- (数论2.1.2)UVA 10168 Summation of Four Primes(欧拉筛法)
- 数论刷题-uva【10168】- Summation of Four Primes
- UVA 10168 Summation of Four Primes(数论)
- UVA 10168 Summation of Four Primes(数论)
- PC/UVa 110705/10168 Summation of Four Primes
- uva 10168 summation of four primes
- UVa Problem 10168 Summation of Four Primes (四素数之和)
- UVa 10168 Summation of Four Primes (“1+1+1+1”问题)
- UVa 10168 - Summation of Four Primes
- UVA10168 Summation of Four Primes【筛选法】
- Uva 10168 Summation of Four Primes 素数
- UVA-10168 Summation of Four Primes
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVA-10168 Summation of Four Primes 哥德巴赫猜想
- uva 10168 Summation of Four Primes(数论-哥德巴赫猜想)
- UVaOJ-10168-Summation of Four Primes 解题报告
- 10168 Summation of Four Primes