【牛客多校】Han Xin and His Troops
题目:
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 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
来源:牛客网
输入
3 100
3 2
5 3
7 2
输出
23
输入
4 10000
12 7
15 2
30 5
8 6
输出
he was definitely lying
输入
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的更多相关文章
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- 2019牛客多校第二场 A Eddy Walker(概率推公式)
2019牛客多校第二场 A Eddy Walker(概率推公式) 传送门:https://ac.nowcoder.com/acm/contest/882/A 题意: 给你一个长度为n的环,标号从0~n ...
- 牛客多校第三场 F Planting Trees
牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...
- 牛客多校第三场 G Removing Stones(分治+线段树)
牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...
- 牛客多校第四场sequence C (线段树+单调栈)
牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
- 2019年牛客多校第一场B题Integration 数学
2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...
- 2020牛客多校第八场K题
__int128(例题:2020牛客多校第八场K题) 题意: 有n道菜,第i道菜的利润为\(a_i\),且有\(b_i\)盘.你要按照下列要求给顾客上菜. 1.每位顾客至少有一道菜 2.给顾客上菜时, ...
随机推荐
- 编写自定义 .NET Core 主机以从本机代码控制 .NET 运行时
自定义 .Net Core 主机运行.Net Core代码,以及控制运行时运行状态,是在.Net Core 高级运行环境以及定制.Net Host ,CLR 等必不可少的. 这些设置包括为 1 ...
- CodeForces 372 A. Counting Kangaroos is Fun
题意,有n只袋鼠,没每只袋鼠有个袋子,大小为si,一个袋鼠可以进入另外一个袋鼠的袋子里面,当且仅当另一个袋鼠的袋子是他的二倍或二倍一上,然后中国袋鼠就是不可见的,不能出现多个袋鼠嵌套的情况.让你求最少 ...
- hdu 6406 Taotao Picks Apples (线段树)
Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n apples o ...
- Apache之——多虚拟主机多站点配置的两种实现方案
Apache中配置多主机多站点,可以通过两种方式实现: 将同一个域名的不同端口映射到不同的虚拟主机,不同端口映射到不同的站点: 将同一个端口映射成不同的域名,不同的域名映射到不同的站点. 我们只需要修 ...
- 跟着大彬读源码 - Redis 9 - 对象编码之 三种list
目录 1 ziplist 2 skiplist 3 quicklist 总结 Redis 底层使用了 ziplist.skiplist 和 quicklist 三种 list 结构来实现相关对象.顾名 ...
- 用jquery实现放大镜效果
----css代码--- *{margin:0;padding:0;} .showimg{position:relative;width:450px;height:420px;border:1px s ...
- HBuilderX使用Vant组件库
HBuilderX使用Vant组件库 HBuilderX是一款由国人开发的开发工具,其官网称其为轻如编辑器.强如IDE的合体版本.但是官方的社区中关于Vant组件的安装大多都是针对微信小程序开发安装V ...
- 算法与数据结构基础 - 排序(Sort)
排序基础 排序方法分两大类,一类是比较排序,快速排序(Quick Sort).归并排序(Merge Sort).插入排序(Insertion Sort).选择排序(Selection Sort).希尔 ...
- Tomcat源码分析 (五)----- Tomcat 类加载器
在研究tomcat 类加载之前,我们复习一下或者说巩固一下java 默认的类加载器.楼主以前对类加载也是懵懵懂懂,借此机会,也好好复习一下. 楼主翻开了神书<深入理解Java虚拟机>第二版 ...
- 为什么选择B+树作为数据库索引结构?
背景 首先,来谈谈B树.为什么要使用B树?我们需要明白以下两个事实: [事实1] 不同容量的存储器,访问速度差异悬殊.以磁盘和内存为例,访问磁盘的时间大概是ms级的,访问内存的时间大概是ns级的.有个 ...