Han Xin and His Troops
中国剩余定理
JAVA板子
/*中国剩余定理,根据公式需要求取大数的逆元*/
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner; public class Main { static BigInteger m[]=new BigInteger[105];
static BigInteger c[]=new BigInteger[105];
static int n;
static BigInteger _m;
static BigInteger x,y;
public static BigInteger[] ex_gcd(BigInteger a,BigInteger b){
BigInteger ans;
BigInteger[] result=new BigInteger[3];
if(b.equals(BigInteger.ZERO))
{
result[0]=a;
result[1]=BigInteger.ONE;
result[2]=BigInteger.ZERO;
return result;
}
BigInteger [] temp=ex_gcd(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]));
//System.out.println(result[0]+" "+result[1]+" "+result[2]);
return result;
}
static BigInteger getInv(BigInteger a,BigInteger md)
{
//x=BigInteger.ZERO;y=BigInteger.ZERO;
BigInteger d[]=ex_gcd(a,md);
//System.out.println(a+" "+md);
//System.out.println(x+" "+y);
//System.out.println(d[1]);
return (d[0].equals(BigInteger.ONE))?(d[1].add(md)).mod(md):BigInteger.valueOf(-1);
}
static BigInteger exCRT(int n)
{
BigInteger m1,m2,c1,c2,d;
for(int i=2;i<=n;i++)
{
m1=m[i-1];m2=m[i];c1=c[i-1];c2=c[i];
d=m1.gcd(m2);
if(!(c2.subtract(c1)).mod(d).equals(BigInteger.ZERO))return BigInteger.valueOf(-1);//此时无法合并
m[i] = m[i-1] .multiply(m[i]).divide(d) ;
BigInteger t=(c2.subtract(c1)).divide(d);
//System.out.println(t);
c[i] = t.multiply( getInv(m1.divide(d),m2.divide(d))) .mod ( m2.divide(d) ).multiply(m1) .add(c1) ;
//System.out.println(c[i]);
// System.out.println(getInv(m1.divide(d),m2.divide(d)));
c[i] = ( (c[i] .mod(m[i])) .add(m[i]) ) .mod( m[i]);
// System.out.println(m[i]);
}
return c[n];
}
public static void main(String[] args) { Scanner sc=new Scanner(System.in);
n=sc.nextInt();
_m=sc.nextBigInteger();
// int k=0;
for(int i=1;i<=n;i++){
m[i]=sc.nextBigInteger();
c[i]=sc.nextBigInteger(); } BigInteger ans=exCRT(n);
//System.out.println(ans);
if(ans.compareTo(_m)>0){
System.out.println("he was probably lying");
}else if(ans.equals(BigInteger.valueOf(-1))){
System.out.println("he was definitely lying");
}else System.out.println(ans); } }
Han Xin and His Troops的更多相关文章
- Han Xin and His Troops(扩展中国剩余定理 Python版)
Han Xin and His Troops(扩展中国剩余定理 Python版) 题目来源:2019牛客暑期多校训练营(第十场) D - Han Xin and His Troops 题意: 看标 ...
- 【牛客多校】Han Xin and His Troops
题目: His majesty chatted with Han Xin about the capabilities of the generals. Each had their shortcom ...
- 2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理
题意 求解 $n$ 个模方程 $x \equiv a (mod \ b)$,不保证模数互素($1 \leq n \leq 100$,$0 \leq b < a< 10^5$). 分析 套扩 ...
- 牛客多校第十场 D Han Xin and His Troops 中国剩余定理
题意: 韩信有若干个兵,给定你若干个模数和余数,再给你一个1e18以内的范围限制,求解同余方程组,如果无解,输出“他一定在撒谎”,如果最小解超出范围限制,输出“他可能在撒谎”,否则输出最小解 注意:不 ...
- 2019牛客暑期多校训练营(第十场) Han Xin and His Troop (高精度+拓展中国剩余定理)
题意 裸题 思路 题中的模数之间并不互质,所以应该用拓展中国剩余定理. 但是交上去会炸,__int128过不了,所以用高精度的板子或者java大数都挺好过的. 这里推荐java大数,因为高精度板子用起 ...
- 2019nc#10
题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A Blackjack 点击查看 背包DP 32/109 补好了 B Coffee Chicken 点击查看 进入讨论 738/2992 通过 ...
- 怎么设置BarTender中二维码大小为25*25
有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...
- 二维码(QR code)基本结构及生成原理
什么是二维码 二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用的码制 ...
- 显示调用dll
原dll中导出的接口如图: Head.h: struct zint_render_line { float x, y, length, width; struct zint_render_line * ...
随机推荐
- java基础/数据加解密(Mooc)
一.消息摘要算法 常用摘要算法: 以下 (HEX)内容:bc指Bouncy Castle | cc指:Apache commons Codec 1.消息摘要算法MD5及MD族(MD2,MD4) 消 ...
- git关联github远程仓库的问题
git关联github远程仓库的时候,报fatal: remote origin already exists. 导致这个问题原因可能是之前关联的时候关联错了,再次关联就不行了. 解决办法是: 1.将 ...
- Python-自定义函数-参数
一.自定义函数参数 1.种类 (1)位置参数 "x"就是位置参数 #!/usr/bin/env python # -*- coding: utf-8 -*- #author: di ...
- 第四周总结 and 实验二
课堂总结 一.课堂笔记总览 1.String类两种实例方法区别 String str1 = "hello";String str2 = "hello"; ...
- 【转】MySQL查询缓存详解
[转]MySQL查询缓存详解 转自:https://www.cnblogs.com/Alight/p/3981999.html 相关文章:http://www.zsythink.net/archive ...
- 分布式理论: CAP、BASE (转)
分布式系统的CAP理论是由Eric Brewer于1999年首先提出的,又被称作布鲁尔定理(Brewer's theorem),CAP是对Consistency(一致性).Availability(可 ...
- 虚拟机的网卡基本配置和基本linux命令
1.切换到/etc/sysconfig/network-script目录 cd /etc/sysconfig/network-scripts 2.将ifcfg-eth0备份成ifcfg-eth0. c ...
- git LF will be replaced by CRLF in hellogit.txt
这个是换行符的问题 参见:https://blog.csdn.net/starry_night9280/article/details/53207928
- [七月挑选]树莓派Raspberrypi上配置Git
title: 树莓派Raspberrypi上配置Git 树莓派Raspberrypi上配置Git. 开始 首先你得有一树莓派!!! 过程 查看自己树莓派的版本 pi@raspberrypi:~ $ u ...
- Kafka在windows下的配置使用
Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实时的处理大量 ...