1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽_J_ Participate in E-sports_Java大數開方

ACM-ICPC 2018 焦作賽區網路預賽_J_ Participate in E-sports_Java大數開方

傳送門

題意:求n和 1+2+3+….+(n-1) 這兩個數字是不是完全平方數。 思路:大數開方,似乎是板子太弱了,在做第二個數字的時候T掉了,然後我就思考下滿足(n-1)*n/2為完全平方數的數字並不多,預處理出來就可以。

import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    static Scanner in = new Scanner(System.in);
    public static int N = 600
; public static void main(String[] args) { BigInteger res[] = new BigInteger[N]; BigInteger one = BigInteger.ONE; BigInteger two = one.add(one); BigInteger six = two.add(two).add(two); res[0]=BigInteger.ZERO; res[1]=one; res[2]=two; for
(int i=3;i<N;i++) { res[i]=six.multiply(res[i-1]).subtract(res[i-2]).subtract(two); } int T = in.nextInt(); while(T-- != 0) { BigInteger n; n = in.nextBigInteger(); int flag1=0,flag2=0; flag1=isSq(n); for
(int i=0;i<N;i++) { if(res[i].compareTo(n)==0) { flag2=1; break; } } if(flag1==1 && flag2==1) { System.out.println("Arena of Valor"); }else if(flag1==0 && flag2==1) { System.out.println("Clash Royale"); }else if(flag1==1 && flag2==0) { System.out.println("Hearth Stone"); }else if(flag1==0 && flag2==0) { System.out.println("League of Legends"); } } } public static int isSq(BigInteger n) { BigInteger l = BigInteger.ZERO; BigInteger r = BigInteger.ONE; while(r.pow(2).compareTo(n) != 1) { l = r; r = r.multiply(BigInteger.valueOf(2)); } while(l.add(BigInteger.valueOf(1)).compareTo(r) == -1) { BigInteger mid = (l.add(r)).divide(BigInteger.valueOf(2)); if(mid.pow(2).compareTo(n) != 1) { l = mid; } else { r = mid; } } BigInteger ans1; if(r.pow(2).compareTo(n) != 1) { ans1 = r; } else { ans1 = l; } if(ans1.multiply(ans1).compareTo(n)==0) return 1; else return 0; } }