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的更多相关文章

  1. Han Xin and His Troops(扩展中国剩余定理 Python版)

    Han Xin and His Troops(扩展中国剩余定理 Python版) 题目来源:2019牛客暑期多校训练营(第十场) D - Han Xin and His Troops 题意:   看标 ...

  2. 【牛客多校】Han Xin and His Troops

    题目: His majesty chatted with Han Xin about the capabilities of the generals. Each had their shortcom ...

  3. 2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理

    题意 求解 $n$ 个模方程 $x \equiv a (mod \ b)$,不保证模数互素($1 \leq n \leq 100$,$0 \leq b < a< 10^5$). 分析 套扩 ...

  4. 牛客多校第十场 D Han Xin and His Troops 中国剩余定理

    题意: 韩信有若干个兵,给定你若干个模数和余数,再给你一个1e18以内的范围限制,求解同余方程组,如果无解,输出“他一定在撒谎”,如果最小解超出范围限制,输出“他可能在撒谎”,否则输出最小解 注意:不 ...

  5. 2019牛客暑期多校训练营(第十场) Han Xin and His Troop (高精度+拓展中国剩余定理)

    题意 裸题 思路 题中的模数之间并不互质,所以应该用拓展中国剩余定理. 但是交上去会炸,__int128过不了,所以用高精度的板子或者java大数都挺好过的. 这里推荐java大数,因为高精度板子用起 ...

  6. 2019nc#10

    题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A Blackjack 点击查看 背包DP 32/109 补好了 B Coffee Chicken 点击查看 进入讨论 738/2992  通过 ...

  7. 怎么设置BarTender中二维码大小为25*25

    有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...

  8. 二维码(QR code)基本结构及生成原理

    什么是二维码 二维码 (2-dimensional bar code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的. 在许多种类的二维条码中,常用的码制 ...

  9. 显示调用dll

    原dll中导出的接口如图: Head.h: struct zint_render_line { float x, y, length, width; struct zint_render_line * ...

随机推荐

  1. uwsgi + nginx 部署python项目(二)

    实现负载均衡 开启两个服务器,nginx负责分发请求到两个服务器,以减轻单个服务器负担. 配置uwsgi服务器 在a项目目录下生成uwsgi.ini文件,在b项目目录下生成uwsgi.ini文件,如何 ...

  2. 如何将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

    cat /etc/issue|tr '[:lower:]' [:upper:] >> /tmp/issue.out

  3. 利用Flot作基于时间段的曲线图

    Flot是一个可以用于绘制多种图表的开源的JS库,Flot本身的功能已经是基本可以满足日常的需要啦,更可喜的是Flot还有很多的插件可以使用,从而为我们提供更加强大的定制功能,本文在作图中使用的显示坐 ...

  4. Python作图包含type3字体解决方案

    1. 解决方案 matplotlib.rcParams[‘text.usetex’] = True

  5. [LeetCode] 211. 添加与搜索单词 - 数据结构设计

    题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...

  6. Sublime Text 3 安装及汉化操作

    Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等.还可自定义键绑定,菜单和工具栏.Sublime Text 的主要功能包括:拼写检查,书签,完整的 ...

  7. Client does not support authentication protocol requested by server; consider upgrading MySQL client

    出现错误 Client does not support authentication protocol requested by server; consider upgrading MySQL c ...

  8. c# 数据库基础(将连接字符串写到配置文件中)

    数据库 操作步骤 一,添加一个配置文件 内容 <?xml version="1.0" encoding="utf-8" ?> <configu ...

  9. 在XCode中使用XCTest

    测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...

  10. visual studio 2013 生成依赖项关系图出错

    开始是说无法连接到sql服务器,我安装卸载localdb http://www.microsoft.com/zh-cn/download/details.aspx?id=29062 下载 CHS\x6 ...