Participate in E-sports【Java大数+二分】

Participate in E-sports
时间限制: 2 Sec 内存限制: 128 MB
提交: 194 解决: 53
[提交] [状态] [命题人:admin]
题目描述
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to make decisions.
They have several boxes of candies, and there are i candies in the i^th box, each candy is wrapped in a piece of candy paper. Jessie opens the candy boxes in turn from the first box. Every time a box is opened, Jessie will take out all the candies inside, finish it, and hand all the candy papers to Justin.
When Jessie takes out the candies in the N^th box and hasn’t eaten yet, if the amount of candies in Jessie’s hand and the amount of candy papers in Justin’s hand are both perfect square numbers, they will choose Arena of Valor. If only the amount of candies in Jessie’s hand is a perfect square number, they will choose Hearth Stone. If only the amount of candy papers in Justin’s hand is a perfect square number, they will choose * Royale. Otherwise they will choose League of Legends.
Now tell you the value of N, please judge which game they will choose.

输入
The first line contains an integer T(1≤T≤800), which is the number of test cases.
Each test case contains one line with a single integer: N(1≤N≤10^200).

输出
For each test case, output one line containing the answer.

样例输入
复制样例数据
4
1
2
3
4
样例输出
Arena of Valor
* Royale
League of Legends
Hearth Stone

题目大意:
输入一个数nnn,记录另一个n1=1+2+3++(n1)=n×(n1)2n1= 1+2+3+\dots+(n-1)=\cfrac{n\times(n-1)}{2}n1=1+2+3+⋯+(n−1)=2n×(n−1)​
完全平方数有:0(02)1(12)4(22)9(32)16(42)0(0^2),1(1^2),4(2^2),9(3^2),16(4^2)\dots0(02),1(12),4(22),9(32),16(42)…
nnn为完全平方数,则记ju1=trueju1=trueju1=true;
n1n1n1为完全平方数,则记ju2=trueju2=trueju2=true;
ju1==true&&ju2==trueju1==true \&\& ju2==trueju1==true&&ju2==true,输出 “Arena of Valor”
ju1==true&&ju2==falseju1==true \&\& ju2==falseju1==true&&ju2==false,输出 “Hearth Stoner”
ju1==false&&ju2==trueju1==false \&\& ju2==trueju1==false&&ju2==true,输出 “* Royale”
ju1==false&&ju2==falseju1==false \&\& ju2==falseju1==false&&ju2==false,输出 “League of Legends”

解题思路:
由于此题nnn很大,所以可以使用Java中的大数来写,而判断一个数是否为完全平方数,可以通过二分来判断。

代码:

import java.math.*;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        Scanner cin=new Scanner(System.in);
        int t=0;
        t=cin.nextInt();
        boolean ju1,ju2;
        while(t!=0) {
            t--;
            BigInteger a=BigInteger.ZERO;
            a=cin.nextBigInteger();
            BigInteger b=BigInteger.ZERO;
            b=a.subtract(BigInteger.ONE);
            b=b.multiply(a);
            BigInteger c=BigInteger.ONE;
            c=c.add(c);
            b=b.divide(c);
            ju1=false;ju2=false;
            if(a.compareTo(BigInteger.ONE)==0) {
                ju1=true;ju2=true;
            }
            else {
                BigInteger l=BigInteger.ONE;
                BigInteger r=a;
                BigInteger mid=BigInteger.ZERO;
                while(r.compareTo(l)>=0) {
                    mid=l.add(r);
                    mid=mid.divide(c);
                    //System.out.println(l+" "+r+" "+mid);
                    BigInteger nape=BigInteger.ZERO;
                    nape=mid.multiply(mid);
                    if(nape.compareTo(a)==0) {
                        ju1=true;
                        break;
                    }
                    else if(nape.compareTo(a)==1) {
                        r=mid.subtract(BigInteger.ONE);
                    }
                    else {
                        l=mid.add(BigInteger.ONE);
                    }
                }
                l=BigInteger.ONE;
                r=b;
                while(r.compareTo(l)>=0) {
                    mid=l.add(r);
                    mid=mid.divide(c);
                    BigInteger nape=BigInteger.ZERO;
                    //System.out.println(l+" "+r+" "+mid);
                    nape=mid.multiply(mid);
                    if(nape.compareTo(b)==0) {
                        ju2=true;
                        break;
                    }
                    else if(nape.compareTo(b)==1) {
                        r=mid.subtract(BigInteger.ONE);
                    }
                    else {
                        l=mid.add(BigInteger.ONE);
                    }
                }
            }
            if(ju1&&ju2) System.out.println("Arena of Valor");
            if(ju1&&!ju2) System.out.println("Hearth Stone");
            if(!ju1&&ju2) System.out.println("* Royale");
            if(!ju1&&!ju2) System.out.println("League of Legends");
        }
    }
}
上一篇:奇异值分解


下一篇:分块矩阵的性质