Lucky Man ZOJ - 4005 (JAVA大整数+牛顿迭代法开根)

Lucky Man

 ZOJ - 4005 

BaoBao is keen on collection. Recently he is abandoning himself to Kantai Collection, especially to collecting cute girls, known as "Fleet Girls".

There are  various types of girls in the game. To get a girl, one can use some materials to build her. The probability to get a type of girl by building is the same for all types of girls. From the Coupon Collector's Problem we know that, to collect all types of girls, the expected number of times of building is .

But this rule does not apply to BaoBao, as he is always luckier than the ordinary players (maybe because he's an European). For BaoBao to collect all types of girls, the expected number of times of building is , where  means the maximum integer that doesn't exceed .

As a lucky man, BaoBao is not interested in the actual value of , and he just wants to know whether  is odd or even. Can you help him?

Input

The first line of the input is an interger  (about 100), indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the number of types of girls.

Output

For each test case, if  is odd output "1" (without quotes), if  is even output "0" (without quotes).

Sample Input

9
2
3
23
233
2333
23333
233333
2333333
23333333

Sample Output

1
1
0
1
0
0
1
1
0

通过打表观察可知只要判断n开根号后的奇偶性就是s的奇偶性,所以题目考查的就是大整数开根号了

 

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

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int t = cin.nextInt();
		BigInteger a, x;
		BigInteger two = new BigInteger("2");
		String s;
		for (int i = 1; i <= t; ++i) {
			a = cin.nextBigInteger();
			s = a.toString();
			x = new BigInteger(s.substring(0, s.length()/2+1));
			while (x.multiply(x).compareTo(a) > 0) {
				x = x.add(a.divide(x)).divide(two);  //x = x - (x^2 - a)/2*x ->  x = (x + a/x)/2
			}
			if (x.mod(two).compareTo(BigInteger.ZERO) == 0) {
				System.out.println("0");
			} else {
				System.out.println("1");
			}
		}
	}
}

牛顿迭代法求解平方根

你可能感兴趣的:(大数,JAVA,SF)