题目:

His majesty chatted with Han Xin about the capabilities of the generals. Each had their shortcomings. His majesty asked, ``How many troops could I lead?" Han Xin replied, ``Your highness should not lead more than 100000." His majesty said, ``And what about you?" ``For your humble servant, the more the merrier!" said Han Xin.

---Records of the Grand Historian

Han Xin was a military general who served Liu Bang during the Chu-Han contention and contributed greatly to the founding of the Han dynasty. Your friend, in a strange coincidence, also named Han Xin, is a military general as well.

One day you asked him how many troops he led. He was reluctant to tell you the exact number, but he told you some clues in certain form, for example:

- if we count the troops by threes, we have two left over;
- if we count by fives, we have three left over;
- if we count by sevens, two are left over;
- ...
You wonder the number of his troops, or he was simply lying. More specifically, you would like to know the minimum possible number of troops he leads, and if the minimum number is too large, then you suspect he was lying.

链接:https://ac.nowcoder.com/acm/contest/890/D
来源:牛客网

示例1

输入

3 100
3 2
5 3
7 2

输出

23
示例2

输入

4 10000
12 7
15 2
30 5
8 6

输出

he was definitely lying
示例3

输入

2 10
11 3
19 7

输出

he was probably lying

题目大意:韩信点兵,给你n个结果,兵队总数%ai=bi,然后求出最后兵队最少到底有多少人。
如果这个数不存在,输出he was definitely lying
如果数量大于给定的门槛m,输出he was probably lying,否则出输出人数。
题解:因为最后的余数并不是完全互质,所以需要使用扩展中国剩余定理。
还有一个坑,就是这些余数可能完全互质,所以最后的M会特别特别大,
我之前使用的是洛谷中国剩余定理模板题的第一个模板,就是因为M特别大,
改写成了java,用大数做就过了。写的有点丑,不要介意。
import java.util.*;
import java.math.*;
import java.security.MessageDigest; public class Main {
static int n;
static BigInteger m,x,y;
static BigInteger[] ai=new BigInteger[200];
static BigInteger[] bi=new BigInteger[200];
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
m=cin.nextBigInteger();
for(int i=1;i<=n;i++)
{
bi[i]=cin.nextBigInteger();
ai[i]=cin.nextBigInteger();
}
BigInteger ans = excrt();
long flag=-1;
if (ans.compareTo(BigInteger.valueOf(flag))==0)
System.out.print("he was definitely lying\n");
else if(ans.compareTo(m)!=-1&&ans.compareTo(m)!=0)
System.out.print("he was probably lying\n");
else System.out.print(ans);
} public static BigInteger excrt()
{
BigInteger k;
BigInteger ans = ai[1];
BigInteger M=bi[1];
for (int i = 2; i <= n; i++)
{
BigInteger a = M, b = bi[i];
BigInteger c=ai[i].subtract(ans.mod(b)).add(b).mod(b);
BigInteger gcd = exgcd(a, b), bg = b .divide(gcd);
long flag=-1;
if (c.mod(gcd).compareTo(BigInteger.ZERO)!=0) return BigInteger.valueOf(flag); x = mul(x,c.divide(gcd), bg);
ans=ans.add(x.multiply(M));
M =M.multiply(bg);
ans = (ans.mod(M).add(M)).mod(M);
}
return (ans.mod(M).add(M)).mod(M);
} public static BigInteger mul(BigInteger a, BigInteger b, BigInteger mod)
{
BigInteger res = BigInteger.ZERO;
while (b.compareTo(BigInteger.ZERO)>0)
{
if (b.mod(BigInteger.valueOf((long)2)).compareTo(BigInteger.ZERO)>0) res = res.add(a).mod(mod);
a = a.add(a).mod(mod);
b = b.divide(BigInteger.valueOf((long)2));
}
return res;
} public 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 tp = x;
x = y; y = tp.subtract(a.divide(b).multiply(y));
return gcd;
}
}

  

贴一下洛谷模板题:https://www.luogu.org/problem/P4777

模板来源:https://blog.csdn.net/niiick/article/details/80229217

C++模板代码:

//niiick
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt; lt read()
{
lt f=,x=;
char ss=getchar();
while(ss<''||ss>''){if(ss=='-')f=-;ss=getchar();}
while(ss>=''&&ss<=''){x=x*+ss-'';ss=getchar();}
return f*x;
} const int maxn=;
int n;
lt ai[maxn],bi[maxn]; lt mul(lt a,lt b,lt mod)
{
lt res=;
while(b>)
{
if(b&) res=(res+a)%mod;
a=(a+a)%mod;
b>>=;
}
return res;
} lt exgcd(lt a,lt b,lt &x,lt &y)
{
if(b==){x=;y=;return a;}
lt gcd=exgcd(b,a%b,x,y);
lt tp=x;
x=y; y=tp-a/b*y;
return gcd;
} lt excrt()
{
lt x,y,k;
lt M=bi[],ans=ai[];//第一个方程的解特判
for(int i=;i<=n;i++)
{
lt a=M,b=bi[i],c=(ai[i]-ans%b+b)%b;//ax≡c(mod b)
lt gcd=exgcd(a,b,x,y),bg=b/gcd;
if(c%gcd!=) return -; //判断是否无解,然而这题其实不用 x=mul(x,c/gcd,bg);
ans+=x*M;//更新前k个方程组的答案
M*=bg;//M为前k个m的lcm
ans=(ans%M+M)%M;
}
return (ans%M+M)%M;
} int main()
{
n=read();
for(int i=;i<=n;++i)
bi[i]=read(),ai[i]=read();
printf("%lld",excrt());
return ;
}

【牛客多校】Han Xin and His Troops的更多相关文章

  1. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  2. 牛客多校第一场 B Inergratiion

    牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...

  3. 2019牛客多校第二场 A Eddy Walker(概率推公式)

    2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...

  4. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

  5. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  6. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

  7. 牛客多校第3场 J 思维+树状数组+二分

    牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...

  8. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

  9. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  10. 2020牛客多校第八场K题

    __int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...

随机推荐

  1. centos7上搭建zookeeper集群

    1.下载zookeeper http://www.apache.org/dyn/closer.cgi/zookeeper/  可以登录这个网站下载,然后上传到 centos上 修改成自己需要的版本 , ...

  2. Django安装 测试、导入项目以及运行开发服务器

    安装Django  下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...

  3. spring boot中的声明式事务管理及编程式事务管理

    这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...

  4. LDAP 服务搭建和后期管理

    LDAP 服务 本文主要在debian配置,如果需要在CentOS上部署,需要修改大部分的路劲,这里需要自行修改. LDAP 服务按照个人理解,也可使理解为一个数据库,但是这个数据库的读写性能不像 M ...

  5. Nginx + Lua 搭建网站WAF防火墙

    前言 对于项目里面只是使用代理等常用功能,在线安装即可,如需制定化模块,则推荐编译安装 PS:本文不仅仅包含Nginx相关的知识点,还包含了逆天学习方法(对待新事物的处理) 官方网站:https:// ...

  6. 一文了解:Redis事务

    Redis事务 事务提供了一种"将多个命令打包,一次性提交并按顺序执行"的机制,提交后在事务执行中不会中断.只有在执行完所有命令后才会继续执行来自其他客户的消息. Redis中的使 ...

  7. 01-Spring Security框架学习

    目录 01-Spring Security框架学习 简介 Spring Security 是什么 Spring Security 解决那些问题 Spring Security 的优点 历史背景 Spr ...

  8. MyBatis 核心配置综述之 ParameterHandler

    目录 ParameterHandler 简介 ParameterHandler 创建 ParameterHandler 中的参数从何而来 ParameterHandler 解析 MyBatis 四大核 ...

  9. RocketMQ中Broker的HA策略源码分析

    Broker的HA策略分为两部分①同步元数据②同步消息数据 同步元数据 在Slave启动时,会启动一个定时任务用来从master同步元数据 if (role == BrokerRole.SLAVE) ...

  10. Oracle RAC 集群启动与停止

    Oracle RAC 启动时,需要使用 root 用户执行,为了方便,写了启动和停止的脚本, 将该脚本放到 /root/bin ,因为bin 目录本身就在环境变量里,所以使用时直接root用户运行脚本 ...