当前位置: 代码迷 >> 综合 >> Goldbach(2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B题)
  详细解决方案

Goldbach(2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 B题)

热度:12   发布时间:2023-11-22 14:12:06.0

题目链接
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
题目来源

2018 ACM-ICPC 中国大学生程序设计竞赛线上赛

解题思路:
Java直接暴力搜索即可

import java.math.BigInteger;
import java.util.Scanner;public class Main {
    public static void main(String[] args) {
    // TODO Auto-generated method stubScanner in = new Scanner(System.in);int num = in.nextInt();BigInteger a,k;for(int i = 0;i < num;i++){
    a = in.nextBigInteger();//定义一个大数for(k = BigInteger.valueOf(2);k.compareTo(a) < 0;k = k.add(BigInteger.ONE)){
    //如果k是素数 && a.subtract(k)是素数,那么就输出,需要注意的是:k.subtract//(certainty)中的参数certainty,表示调用者愿意忍受的不确定性的度量:如果该//数是素数的概率超过了1- 1/2**certainty方法,则该方法返回true一般来说定为//10就差不多,定为100可能会内存超限制if(Isprime(k) && Isprime(a.subtract(k))){
    System.out.println(k + " " + (a.subtract(k)));break;}}				}}//定义函数(显得更加清晰明了),一定用static(静态的),单纯的判断就可以了,(对Java了解不深,//不知道为什么定义为static,有知道的可以指导下,谢谢!(抱拳了)static boolean Isprime(BigInteger a){
    if(a.isProbablePrime(10))return true;return false;}
}
  相关解决方案