The Preliminary Contest for ICPC Asia Xuzhou 2019 A. Who is better?(java大数求中国剩余定理+斐波那契博弈模板)

题意:

类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量*2之间的数量,不能拿时则输

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	public static BigInteger x = BigInteger.ZERO, y = BigInteger.ZERO;
	public static BigInteger[] ai = new BigInteger[110];
	public static BigInteger[] bi = new BigInteger[110];
	public static BigInteger[] fi = new BigInteger[110];
	public static int n;

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		fi[1]=BigInteger.valueOf(2);
		fi[2]=BigInteger.valueOf(3);
		for(int i=3; i <= 100; i++) {
			fi[i] = fi[i-1].add(fi[i-2]);
		}
		n = cin.nextInt();
		for (int i = 1; i <= n; i++) {
			bi[i] = cin.nextBigInteger();
			ai[i] = cin.nextBigInteger();
		}
		BigInteger ans = excrt();
		if(ans.compareTo(BigInteger.valueOf(-1)) == 0)
			System.out.println("Tankernb!");
		else {
			int f=0;
			for(int i=2;i<=100;i++)
				if(ans.compareTo(fi[i])==0)
					f=1;
			if(f==1)
				System.out.println("Lbnb!");
			else 
				System.out.println("Zgxnb!");
		}
	}

	private static BigInteger excrt() {
		BigInteger M = bi[1], ans = ai[1];
		for (int i = 2; i <= n; i++) {
			BigInteger a = M, b = bi[i], c = ai[i].subtract(ans.mod(b)).add(b).mod(b);
			BigInteger gcd = exgcd(a, b), bg = b.divide(gcd);
			if (c.mod(gcd).compareTo(BigInteger.ZERO) != 0)
				return BigInteger.valueOf(-1);
			x = x.multiply(c.divide(gcd));
			ans = ans.add(x.multiply(M));
			M = M.multiply(bg);
			ans = ans.mod(M).add(M).mod(M);
		}
		ans = ans.mod(M).add(M).mod(M);
		return ans;
	}

	private static BigInteger exgcd(BigInteger a, BigInteger b) {
		if (b.compareTo(BigInteger.ZERO) == 0) {
			x = BigInteger.ONE;
			y = BigInteger.ZERO;
			return a;
		}
		BigInteger gcd = exgcd(b, a.mod(b));
		BigInteger k = x;
		x = y;
		y = k.subtract(a.divide(b).multiply(y));
		return gcd;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值