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

8 篇文章 0 订阅
6 篇文章 0 订阅

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");
			}
		}
	}
}

牛顿迭代法求解平方根

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值