计蒜客2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B. Goldbach 哥德巴赫猜想

Description:

Goldbach's conjecture is one of the oldest and best-known unsolved problems in number theory and all of mathematics. It states:

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

The actual verification of the Goldbach conjecture shows that even numbers below at least 1e14 can be expressed as a sum of two prime numbers. 

Many times, there are more than one way to represent even numbers as two prime numbers. 

For example, 18=5+13=7+11, 64=3+61=5+59=11+53=17+47=23+41, etc.

Now this problem is asking you to divide a postive even integer n (2<n<2^63) into two prime numbers.

Although a certain scope of the problem has not been strictly proved the correctness of Goldbach's conjecture, we still hope that you can solve it. 

If you find that an even number of Goldbach conjectures are not true, then this question will be wrong, but we would like to congratulate you on solving this math problem that has plagued humanity for hundreds of years.

Input:

The first line of input is a T means the number of the cases.

Next T lines, each line is a postive even integer n (2<n<2^63).

Output:

The output is also T lines, each line is two number we asked for.

T is about 100.

本题答案不唯一,符合要求的答案均正确

样例输入

1
8

样例输出

3 5

哥德巴赫猜想:

任一大于2的偶数都可写成两个素数之和,亦称为强哥德巴赫猜想关于偶数的哥德巴赫猜想


在比赛的时候显示看见数据量吓了一跳,之后用程序试着输出了一下1e9左右的偶数可以分成的数,发现n这个偶数可以分成的两个素数和n/2的差不会很大所以直接暴力往两边找就可以。

检测素数的时候数据打的时候直接找会超时,我是用的java大数里的函数。c++的话可以用拉宾素数检测的模板,Java判断素数函数应该也是用的拉宾素数检测。


赛后我又仔细看了一下哥德巴赫猜想发现,哥德巴赫猜想分成弱哥德巴赫猜想和强哥德巴赫猜想。

强哥德巴赫猜想:

任一大于2的偶数都可写成两个素数之和,亦称为强哥德巴赫猜想关于偶数的哥德巴赫猜想

用代码写的话可以暴力将偶数除以2得到的结果往两边扩。在longlong内不会超时。

弱哥德巴赫猜想:

任一大于7的奇数都可写成三个质数之和的猜想。后者称为弱哥德巴赫猜想关于奇数的哥德巴赫猜想

把这个奇数-3,就可以变成一个偶数就变成了 => 3+偶数 => 3+ “强哥德巴赫猜想所以也可以暴力来写。

代码:


import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int t;
		t=cin.nextInt();
		while (t--!=0) {
			long n;
			n=cin.nextLong();
			long temp=n;
			n=n/2;
			for (long i=0;i<10000;i++) {
				long x=n-i;
				long y=n+i;
				if (BigInteger.valueOf(x).isProbablePrime(1)&&BigInteger.valueOf(y).isProbablePrime(1)) {
					System.out.println(x+" "+y);
					break;
				}
			}
		}
		
	}
}






猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/80055421