@ACM-ICPC 2018 焦作赛区网络预赛 J:Participate in E-sports (Java大数,牛顿迭代式)

题目链接:https://www.jisuanke.com/contest/1558/107313

 

吐槽:   - - - -  队友牛X  ..   全靠队友.....  这题 java   很恶心.

题意很明确:    判断  n  和 n*(n-1)/2  是否是完全平方数...

主要超时 在  判断完全平方...

 

 

代码:

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

public class Main {
    public static BigInteger one = BigInteger.ONE;
    public static BigInteger two = new BigInteger("2");
    public static void init()
    {
        /*BigInteger p1 = new BigInteger("9");
        BigInteger p2 = new BigInteger("289");
        for(BigInteger n = p1 ; n.compareTo(new BigInteger("100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))<=0;)
        {
            n = BigInteger.valueOf(34).multiply(p1).subtract(p2).subtract(BigInteger.valueOf(16));
            p2 = p1;
            p1 = n;
            System.out.println(n+",");
        }*/
//        BigInteger p1 = new BigInteger("2");
//        BigInteger p2 = new BigInteger("289");
//        for(BigInteger x  = one; p1.compareTo(new BigInteger("100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))<=0;x=x.add(one)) {
//            p1 = two.multiply(x.pow(2)).subtract(BigInteger.valueOf(20).multiply(x).add(two));
//            p2 = one.subtract(x).multiply(one.subtract(BigInteger.valueOf(34).multiply(x)).add(x.pow(2)));
//            p1 = p1.divide(p2);
//            System.out.println(p1 + ",");
//        }
    }
    public static void main(String[] args){
      // init();
        int T;
        Scanner cin =new Scanner(System.in);
        T = cin.nextInt();
        while(T--!=0)
        {
            {
                BigInteger n = cin.nextBigInteger();
                BigInteger sumN = n.multiply(n.subtract(one)).shiftRight(1);
                // System.out.println(sumN);
                {
                    boolean f1 = is_square(n);
                    boolean f2 = is_square(sumN);
                    if (f1 == true && f2 == true) {
                        System.out.println("Arena of Valor");
                    } else if (f1 == false && f2 == false) {

                        System.out.println("League of Legends");
                    } else if (f1 == true && f2 == false) {

                        System.out.println("Hearth Stone");
                    } else if (f1 == false && f2 == true) {

                        System.out.println("Clash Royale");
                    }
                }
            }
        }
    }
    public static boolean is_square(BigInteger n){
        String a = n.toString();
        if(n.toString().length()%2==0)
            a = a.substring(0,n.toString().length()/2+1);
        else
            a = a.substring(0,(1+ n.toString().length())/2);
        BigInteger x = new BigInteger(a);
        BigInteger Two=new BigInteger("2");
        if(a =="1")
        {
            x = one;
        }else{
            while(n.compareTo(x.multiply(x))<0){
                x=(x.add(n.divide(x))).shiftLeft(1);
            }
           // System.out.println(x);
        }

        if(x.pow(2).compareTo(n) == 0)
            return true;
        else
            return false;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值