作者:mobiledu2502875697 | 来源:互联网 | 2023-10-12 09:39
给定一个素数p ,问是否存在两个数的里方差就是p
p<=1e12
经过简单的数学推导,可以很快想到只要 方程 3 * b *b + 3 * b + 1 - p = 0 关于b有解即可。
简单的想法就是二分
但是问题是b*b要爆long long,咋办呢?
java呗
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.math.*;
import java.io.*;
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.Math.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
/*
static BigInteger quickPower(BigInteger a, BigInteger b) {
BigInteger base = a;
BigInteger ans = BigInteger.ONE;
while(b.compareTo(BigInteger.ZERO) > 0){
if(b .and(BigInteger.ONE).equals(BigInteger.ONE)) ans = ans.multiply(base);
base = base.multiply(base);
b = b.divide(BigInteger.valueOf(2));
}
return ans;
}*/
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int T;
BigInteger p;
T = in.nextInt();
while(T-- > 0){
p = in.nextBigInteger();
BigInteger l = p.multiply(BigInteger.valueOf(-1)), r = p;
int f = 0;
while(r.compareTo(l) > 0){
BigInteger mid = l.add(r);
mid = mid.divide(BigInteger.valueOf(2));
BigInteger tmp = mid.multiply(mid).multiply(BigInteger.valueOf(3));
tmp = tmp.add(mid.multiply(BigInteger.valueOf(3)));
tmp = tmp.add(BigInteger.ONE);
if(tmp.compareTo(p) == 0) {
f = 1;
break;
}
if(tmp.compareTo(p) > 0) r = mid;
else l = mid.add(BigInteger.ONE);
}
if(f == 0) System.out.println("NO");
else System.out.println("YES");
}
in.close();
}
}