【牛客多校】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.给顾客上菜时, ...
随机推荐
- Restful API 中的错误处理
简介 随着移动开发和前端开发的崛起,越来越多的 Web 后端应用都倾向于实现 Restful API. Restful API 是一个简单易用的前后端分离方案,它只需要对客户端请求进行处理,然后返回结 ...
- apache bench的简单使用
ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求. 需要针对web做压力测试,所以简单学习了一 ...
- 重入锁的学习 (ReentrantLock)
重入锁 :(ReentrantLock) 上锁 用reentrantLock.lock 方法 解锁 用reentrantLock.unlock 方法 上锁和解锁 必须配对 可以多重上锁 Reentr ...
- HackBar收费版绕过
一段时间没用HackBar,近期做渗透,打开火狐浏览器,按F12键调出HackBar,发现居然需要收费买license才能使用. 经过研究,整理了以下两个绕过HackBar收费版的方法. 第一种:用其 ...
- 使用nginx代理centos yum 源
我们在安装centos 服务器时,可能会有以下情况: 局域网内有若干台服务器,但是只有一台服务器可以连接外网,其余服务器都不可以连接外网,但通过局域网与外网机器联通. 那么我们再使用 yum 安装软件 ...
- SpringMVC学习笔记之---数据绑定
SpringMVC数据绑定 一.基础配置 (1)pom.xml <dependencies> <dependency> <groupId>junit</gro ...
- 多态、继承、this、super
先放一下多态的定义: (360词典上的哈) 多态(Polymorphism)按字面的意思就是"多种状态".在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie C ...
- MySQL高速缓存
MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1,默认配置下,MySQL的该功能是没有启动的,可能你通过show variables ...
- .netcore持续集成测试篇之测试方法改造
系列目录 通过前面两节讲解,我们的测试类中已经有两个测试方法了,总体上如下 public class mvc20 { private readonly HttpClient _client; publ ...
- DC6-靶机渗透
靶场下载链接: Download: http://www.five86.com/downloads/DC-6.zip Download (Mirror): https://download.vulnh ...