这是 meelo 原创的 IEEEXtreme极限编程大赛题解

Xtreme 10.0 - Goldbach's Second Conjecture

题目来源 第10届IEEE极限编程大赛

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/goldbachs-second-conjecture

An integer p > 1 is called a prime if its only divisors are 1 and p itself. A famous conjecture about primes is Goldbach's conjecture, which states that

Every even integer greater than 2 can be expressed as the sum of two primes.

The conjecture dates back to the year 1742, but still no one has been able to come up with a proof or find a counterexample to it. We considered asking you prove it here, but realized it would be too easy. Instead we present here a more difficult conjecture, known as Goldbach's second conjecture:

Every odd integer greater than 5 can be expressed as the sum of three primes.

In this problem we will provide you with an odd integer N greater than 5, and ask you to either find three primes p1p2p3 such that p1 + p2 + p3 = N, or inform us that N is a counterexample to Goldbach's second conjecture.

Input Format

The input contains a single odd integer 5 < N ≤ 1018.

Output Format

Output three primes, separated by a single space on a single line, whose sum is N. If there are multiple possible answers, output any one of them. If there are no possible answers, output a single line containing the text "counterexample" (without quotes).

Sample Input

65

Sample Output

23 31 11

Explanation

In the sample input N is 65. Consider the three integers 11, 23, 31. They are all prime, and their sum is 65. Hence they form a valid answer. That is, a line containing "11 23 31", "23 31 11", or any permutation of the three integers will be accepted. Other possible answers include "11 37 17" and "11 11 43".

题目解析

将一个奇数分解为三个质数,奇数最大有1018。可以遍历前两个质数,然后判断奇数与两个质数的差是否仍未质数。如果3个质数都有1017,那么肯定会超时。

事实上是,存在解前两个质数都不超过1000。这个时候关键的问题成为了,如何判断一个规模有1018的数为质数。常规的方法复杂度为O(sqrt(n)),会超时。这时候需要一点数论的知识,Miller–Rabin质数测试能够在O((logn)2)判断一个数是否为质数。算法在维基百科详细的介绍。下面程序里的Miller–Rabin质数测试使用的是github上的代码。

程序

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std; #define MAXN 1000
typedef unsigned long long ULL;
typedef long long LL; bitset<MAXN> nums;
int primes[MAXN];
int num_prime = ; void getPrimes(long long max) { // get all primes under max
for(int i=; i<=sqrt(max+0.5); i++) {
if(nums[i] == false) {
primes[num_prime] = i;
num_prime++;
for(long long n=*i; n<max; n+=i) {
nums[n] = true;
}
}
}
for(int i=int(sqrt(max+0.5))+; i<max; i++) {
if(nums[i] == false) {
primes[num_prime] = i;
num_prime++;
}
}
} LL MultiplyMod(LL a, LL b, LL mod) { //computes a * b % mod
ULL r = ;
a %= mod, b %= mod;
while (b) {
if (b & ) r = (r + a) % mod;
b >>= , a = ((ULL) a << ) % mod;
}
return r;
}
template<typename T>
T PowerMod(T a, T n, T mod) { //computes a^n % mod
T r = ;
while (n) {
if (n & ) r = MultiplyMod(r, a, mod);
n >>= , a = MultiplyMod(a, a, mod);
}
return r;
}
template<typename T>
bool isPrime(T n) {
//determines if n is a prime number using Miller–Rabin primality test
// from https://github.com/niklasb/tcr/blob/master/zahlentheorie/NumberTheory.cpp
const int pn = , p[] = { , , , , , , , , };
for (int i = ; i < pn; ++i)
if (n % p[i] == ) return n == p[i];
if (n < p[pn - ]) return ;
T s = , t = n - ;
while (~t & )
t >>= , ++s;
for (int i = ; i < pn; ++i) {
T pt = PowerMod<T> (p[i], t, n);
if (pt == ) continue;
bool ok = ;
for (int j = ; j < s && !ok; ++j) {
if (pt == n - ) ok = ;
pt = MultiplyMod(pt, pt, n);
}
if (!ok) return ;
}
return ;
} int main() {
long long n;
cin >> n; getPrimes(MAXN); for(int i=; i<num_prime; i++) {
for(int j=i; j<num_prime; j++) {
if(isPrime(n-primes[j]-primes[i])) {
printf("%lld %lld %lld", primes[i], primes[j], n-primes[i]-primes[j]);
return ;
} }
} return ;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Goldbach's Second Conjecture的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  4. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  6. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  7. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  8. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  9. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

随机推荐

  1. 【hdu4035】Maze

    Portal --> hdu4035 Solution 讲道理不是很懂为啥概d那么喜欢走迷宫qwq (推式子推的很爽的一题?) 首先大力dp列式子 用\(f[i]\)表示从\(i\)到离开的期望 ...

  2. c++ 智能指针(转)

    智能指针的使用 智能指针是在 <memory> 标头文件中的 std 命名空间中定义的. 它们对 RAII 或“获取资源即初始化”编程惯用法至关重要. 此习惯用法的主要目的是确保资源获取与 ...

  3. 在Android 下写一个检测软件版本号 以自动升级APP 的插件

    直接上图上代码: 1.插件类的编写 工程目录结构图: 代码如下: package org.apache.cordova.versionupdate; import org.apache.cordova ...

  4. python 分享文件

    http://note.youdao.com/noteshare?id=1787e8bf3a71fca16005ece3e7fffb6c

  5. 线上Redis偶发性链接失败排查记

    问题过程 输入法业务于12月12日上线了词库接受业务,对部分用户根据用户uuid判断进行回传,在12月17日早上8点多开始出现大量的php报错(Redis went away),报错导致了大量的链接积 ...

  6. Debian sudo自动补全

    解决 debian sudo TAB 键不能自动补全命令的原因 一般情况,命令行输入 sudo apt-get ins 按 tab ,它后面会自动补全为 install 如果右面写了包的名的一部分,按 ...

  7. CF757 C hash

    一种数字可以变成另一种数,要求每组中变换前后各种数字数量不变,问方案数 对现有每组中的每个数字构造出现在各个组情况的序列,如2 出现在第一组和第二组各一次那么就要加入组别的标号1,2,出现重复次仍要加 ...

  8. Centos7系统环境下Solr之Java实战(一)搭建solr服务器

    搭建步骤 1.分别上传tomcat.sorl到指定文件夹并解压 2.把solr部署到Tomcat下 通过命令 cp apache-tomcat-7.0.47 /usr/local/sorl/tomca ...

  9. 23、Xpath

    1.什么是Xpath?1.XPath即为XMLPath的简称,它是一种用来确定XML文档中某部分位置的语言.2.HTML可以看做是XML的一种实现,所以selenium用户可以使用这种强大的语言在we ...

  10. css3旋转、过渡、动画属性

    1.transform 该属性对元素进行旋转.缩放.移动和倾斜 translate元素从当前位置移动 rotate元素顺时针旋转 scale元素的尺寸增大或减小 skew元素翻转 2.transiti ...