中国剩余定理 非互质(java大数)

import java.math.BigInteger;
import java.util.Scanner;
 
public class Main { 
	static BigInteger dd;
	static BigInteger  xx;
	static BigInteger yy;
	static BigInteger []a = new BigInteger [1005];
	static BigInteger []r = new BigInteger [1005];
	static BigInteger o = BigInteger.ONE;
	static BigInteger z = BigInteger.ZERO; 
	static long f [] = new long [100];
	public static BigInteger[] ex_gcd1(BigInteger a,BigInteger b)	{ 
		BigInteger ans; BigInteger[] result=new BigInteger[3]; 
		if(b.compareTo(BigInteger.ZERO) == 0) { 
			result[0]=a;
			result[1]=o; 
			result[2]=z; 
			return result;
		} 
		BigInteger [] temp=ex_gcd1(b,a.mod(b)); 
		ans = temp[0]; 
		result[0]=ans; 
		result[1]=temp[2];
		result[2]=temp[1].subtract((a.divide(b)).multiply(temp[2]));
		return result; 
	} 
	static BigInteger Chinaa(int len) { //n % a[i] = r[i]
		BigInteger M=a[0],R=r[0];
		for(int i=1; i<len; i++) {
			BigInteger []result =  ex_gcd1(M,a[i]);
			dd = result[0];
			xx = result[1];
			yy = result[2];  
			if((R.subtract(r[i])).mod(dd).compareTo(BigInteger.ZERO)!=0) return BigInteger.valueOf(-1);
			BigInteger xxx = xx;
			xx=(R.subtract(r[i])).divide(dd);
			xx = xx.multiply(xxx).mod(a[i]);
			R= R.subtract(xx.multiply(M));
			M=M.divide(dd).multiply(a[i]);
			R= R.mod(M);
		}
		return (R.mod(M).add(M)).mod(M);
	}  
	static Scanner cin = new Scanner(System.in);
	public static void main(String[] args) {
		  f[0]=1;
		  f[1]=1;
		  for (int i = 2; i <= 80;i++) {
			  f[i]=f[i-1]+f[i-2];
		  }
		  int k;
		  k=cin.nextInt();
		  if(k<=0) {
			  System.out.println("Tankernb!");
			  return;
		  }
		  for (int i = 0; i < k;i++) {
			  a[i]=cin.nextBigInteger();
			  r[i]=cin.nextBigInteger();
		  }
		  BigInteger w=Chinaa(k);
		  //System.out.println(w);
		  if(w.compareTo(BigInteger.valueOf(1))<=0||w.compareTo(BigInteger.valueOf(1000000000000000l))>0) {
			  System.out.println("Tankernb!");
		  }
		  else {
			  boolean flag=false;
			  for (int i = 0; i <= 80;i++) {
				  if(w.compareTo(BigInteger.valueOf(f[i]))==0) {
					flag=true;  
					break;
				  }
			  }
			  if(flag) System.out.println("Lbnb!");
			  else System.out.println("Zgxnb!");
		  }
			
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值