UVaOJ-10168-Summation of Four Primes 解题报告
来源:互联网 发布:mmorpg推荐 知乎 编辑:程序博客网 时间:2024/05/16 17:08
哥德巴赫猜想推广。题意:给一个数,如果它能被分解成四个素数的和,那么输出这四个素数,如果不能,输出指定信息。
我的解题思路:由于一个大于2的偶数能够被分解成两个素数的和,因此可以推出一个大于8的数必定能分解成4个素数的和。首先如果这个大于8的数是偶数,那么它能够被分解成两个偶数的和,再把这两个偶数根据哥德巴赫猜想分解,如果是奇数的话,那么它必定能分解成2,3和一个偶数的和,在把这个偶数分解就行了。
我的解题代码:
#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <algorithm>using namespace std;const int N = 10000001;bool isprime[N];int primes[N], pn;int num;void InitRead();void DataProcess();void FastSieve(int maxn);int main(){ InitRead(); while (~scanf("%d", &num)) { DataProcess(); } return 0;}void InitRead(){ pn = 0; memset(isprime, true, sizeof(isprime)); isprime[0] = isprime[1] = false; FastSieve(N - 1); return;}void DataProcess(){ if (num < 8) { puts("Impossible."); return; } int x; if (num % 2 == 0) { x = num - 4; printf("2 2 "); } else { x = num - 5; printf("2 3 "); } for (int i=0; i<pn; ++i) { if (isprime[x - primes[i]]) { printf("%d %d\n", primes[i], x - primes[i]); break; } } return;}void FastSieve(int maxn){ for (int i=2; i<=maxn; ++i) { if (isprime[i]) primes[pn++] = i; for (int j=0; j<pn; ++j) { if (i * primes[j] > maxn) break; isprime[i * primes[j]] = false; if (i % primes[j] == 0) break; } } return;}
0 0
- UVaOJ-10168-Summation of Four Primes 解题报告
- 10168 Summation of Four Primes
- 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
- UVa 10168 Summation of Four Primes
- 110705 Summation of Four Primes
- Summation of Four Primes - PC110705
- UVa Problem Solution: 10168 - Summation of Four Primes
- PC/UVa 110705/10168 Summation of Four Primes
- (数论2.1.2)UVA 10168Summation 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
- k近邻法:R实现(二)
- 代码对齐
- java判断整数、小数和小数点开头很简单的方法
- 显示公历农历干支的Delphi控件: LssCalendar 2.0
- android开发之常见的android开发错误及解决方案
- UVaOJ-10168-Summation of Four Primes 解题报告
- 定时重启domino的批处理程序
- 我的办公助手(OfficeAssist) V0.1: 一个能查看农历和节假日的小工具,带提醒功能
- ubuntu安装eclipse
- Android 手势密码分析
- uva1398
- contiki 源码分析之tunslip6(七)(exampes)
- SAX解析XML
- ZOJ 1586 QS Network prim优化模板