UVa 10311-Goldbach and Euler
来源:互联网 发布:济南泉方数据库 编辑:程序博客网 时间:2024/06/05 14:48
这个题很好,求100000000以内的数是否可以写成两个素数的和,要求两个素数的差最小。对于输入的Case。
如果Case是偶数的话,我们从Case/2向前开始枚举就可以,如果满足条件直接输出即可。
如果Case是奇数的话,我们知道素数只有2是偶数,其余的素数都是奇数。一个奇数如果可以写成两个素数和的情况,其中一个一定是2,然后再判断Case - 2是不是素数即可。(因为奇数-奇数一定得偶数,偶数除了2之外一定不是素数)。
/************************************************************************* > File Name: 10311.cpp > Author: Toy > Mail: ycsgldy@163.com > Created Time: 2013年06月02日 星期日 14时03分40秒 ************************************************************************/#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <cstdlib>#include <climits>#include <sstream>#include <fstream>#include <cstdio>#include <string>#include <vector>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>using namespace std;const int INF = 0x7fffffff;typedef pair<int,int> II;typedef vector<int> IV;typedef vector<II> IIV;typedef vector<bool> BV;typedef long long i64;typedef unsigned long long u64;typedef unsigned int u32;#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))const int MAXP = 100000000;const int SQRP = 10000; //sqrt(MAX)int _c[(MAXP>>6)+1];void prime_sieve ( ) {for ( int i = 3; i <= SQRP; i += 2 ) if ( !IsComp ( i ) ) for ( int j = i * i; j <= MAXP; j += i + i ) SetComp ( j );}bool is_prime ( int n ) {if ( n < 2 || ( n > 2 && n % 2 == 0 ) ) return false;return !IsComp ( n );}int Case;int main ( ) {prime_sieve ( );while ( scanf ( "%d", &Case ) == 1 ) {if ( Case % 2 == 0 ) {int left, right;bool flag = 0;for ( int i = Case / 2; i >= 2; --i ) if ( is_prime ( i ) && is_prime ( Case - i ) && i != ( Case - i ) ) {left = i, right = Case - i, flag = 1;break;}if ( flag ) printf ( "%d is the sum of %d and %d.\n", Case, left, right );else printf ( "%d is not the sum of two primes!\n", Case );}else {if ( is_prime ( Case - 2 ) ) printf ( "%d is the sum of 2 and %d.\n", Case, Case - 2 );else printf ( "%d is not the sum of two primes!\n", Case );}} return 0;}
- UVa 10311-Goldbach and Euler
- UVa 10311 - Goldbach and Euler
- 10311 - Goldbach and Euler
- UVa10311 - Goldbach and Euler
- UVa 543 - Goldbach's Conjecture
- UVa 543-Goldbach's Conjecture
- UVA 543 Goldbach's Conjecture
- UVA 10735 Euler Circuit
- Uva-10735-Euler Circuit
- UVa 10735 Euler Circuit
- UVA 1073 Euler Circuit
- Project Euler:Problem 46 Goldbach's other conjecture
- UVa 686 - Goldbach's Conjecture (II)
- Goldbach
- UVa 543 Goldbach's Conjecture (素数&哥德巴赫猜想)
- UVa 686 Goldbach's Conjecture (II) (哥德巴赫猜想)
- UVA 1342 That Nice Euler Circuit
- UVa 10129 Play on Words ( euler path )
- 亲,你遇到this package depends on missing sdk platform 问题了吗
- Android应用开发--MP3音乐播放器滚动歌词实现
- Ant 命令详解
- HTTP协议详解(真的很经典)
- 【win 7系统下电脑竟然“失眠”,如何解决?】
- UVa 10311-Goldbach and Euler
- c++面试
- 【Android】简约而不简单Android SimpleAdapter
- gvim的纵向编辑模式
- csapp2e 家庭作业 3.65
- Minor GC 和 Full GC输出日志格式
- 关于MFC未来的感悟
- Eclipse+Maven+Mybatis使用
- c面试