2018 ACM-ICPC B. Goldbach【java快速判断素数】

xiaoxiao2021-02-28  13

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

思路:

此题用java大数类里的isProbablePrime(1)判断素数。

代码:

import java.util.*; import java.math.*; public class Main { static BigInteger zero = BigInteger.ZERO; static BigInteger one = BigInteger.ONE; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int cas = cin.nextInt(); while(cas--!=0){ BigInteger n = cin.nextBigInteger(); BigInteger half = n.divide(BigInteger.valueOf(2)); if(n.compareTo(BigInteger.valueOf(4))==0){ System.out.println("2 2"); continue; } for(BigInteger i = zero.max(half.subtract(BigInteger.valueOf(10000))); i.compareTo(n.min(half.add(BigInteger.valueOf(10000))))<0; i = i.add(one)){ if(n.subtract(i).isProbablePrime(1) && i.isProbablePrime(1)){ System.out.println(i + " " + n.subtract(i)); break; } } } } }
转载请注明原文地址: https://www.6miu.com/read-1950385.html

最新回复(0)