ZOJ 4005 (大数+手动开根号)

Lucky Man

Time Limit:2 Seconds      Memory Limit: 65536 KB

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 theCoupon 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
 

110100110

题意:
化简之后的题意就是给你一个数,让你开根号取结果的整数部分,如果整数部分是奇数输出1,否则输出0
解析:
牛顿迭代法公式:
X(n+1)=[X(n)+p/Xn]/2
这道题时间卡的很紧,我用python的大数+二分开根号才过(看了网上的代码),但用牛顿迭代法开根号一直会出现 Non-zero Exit Code问题
java做的话我T了
下面用的二分里面全是整数
def  sqrt(n):
    l=1
    r=n
    while(l<=r):
        mid=(l+r)>>1
        F=mid*mid   //这里要先储存起来,不然在if里面乘的话会更耗时间
        if F < n:
            l=mid+1
        elif F > n:
            r=mid-1;
        else:
            return mid
    return r


t=int(raw_input())
for i in range(t):
    n=int(input())
    if(n==0):
        print(0)
        continue
    r=sqrt(n)
    print(int(r&1))

这里贴一个T 的java代码,做模板
package zojmonth;

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

public class Main {

	public static BigInteger myBigNumSqrt(BigInteger xx)
	{
	BigDecimal x=new BigDecimal(xx);
	BigDecimal n1=BigDecimal.ONE;
	BigDecimal ans=BigDecimal.ZERO;
	//int i=1;
	while((n1.multiply(n1).subtract(x)).abs().compareTo(BigDecimal.valueOf(0.001))==1)
	{
	//System.out.println(i+"..."+n1);
	//i++;
	BigDecimal s1=x.divide(n1,2000,BigDecimal.ROUND_HALF_UP);
	BigDecimal s2=n1.add(s1);
	n1=s2.divide(BigDecimal.valueOf(2),2000,BigDecimal.ROUND_HALF_UP);
	}
	ans=n1;
	//System.out.println(ans);
	BigInteger rt =new BigInteger(ans.toString().split("\\.")[0]);
	return rt;
	}

	
	public static void main (String[] args){
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		for(int k=0;k<t;k++)
		{
			BigInteger n=sc.nextBigInteger();
			if(n.compareTo(BigInteger.ONE)==0) 
			{
				System.out.println(0);
				continue;
			}
			BigInteger tmp=myBigNumSqrt(n);
			//System.out.println(tmp);
			if(tmp.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0) //大数取模
			{
				System.out.println(1);
			}
			else
			{
				System.out.println(0);
			}
		}
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值