【牛客多校】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.给顾客上菜时, ...
随机推荐
- vscode vue开发环境搭建
以前仅了解过VUE但没有真正上手过,现在因为工作需要准备再近几个月里系统的学习一下这款超火的前端框架,希望大佬们指教. ---------------------------------------- ...
- 解释一下一门语言该有的东东(Javascript)
注释 Js中有两种注释 // 单行注释 /**/ 多行注释 变量 变量就像学校学习的 未知数 如 3 + x = 8 x: 类似变量,在改造一下 x + y = z 当 x=3, y=5, z=8, ...
- java 动手动脑7
---恢复内容开始--- 一.动手动脑:多层的异常捕获-1 阅读以下代码(CatchWho.java),写出程序运行结果: ArrayIndexOutOfBoundsException/内层try-c ...
- 编码规范 | Java函数优雅之道(上)
导读 随着软件项目代码的日积月累,系统维护成本变得越来越高,是所有软件团队面临的共同问题.持续地优化代码,提高代码的质量,是提升系统生命力的有效手段之一.软件系统思维有句话“Less coding, ...
- 初识代理——Proxy
无处不在的模式——Proxy 最近在看<设计模式之禅>,看到代理模式这一章的时候,发现自己在写spring项目的时候其实很多时候都用到了代理,无论是依赖注入.AOP还是其他,可以说是无处不 ...
- 【TCP/IP】ICMP协议
ICMP协议有两种报文: 1,查询报文 2,差错报文
- No!No!No! It's not fashion!
还记得搞怪的hold住姐Miss Lin么,对于人们常规的行为,Miss Lin会挑起夸张的眉毛说:"Oh my God, it's not fashion!".如果程序员圈子里有 ...
- Sql Or NoSql,看完这一篇你就懂了
前言 你是否在为系统的数据库来一波大流量就几乎打满CPU,日常CPU居高不下烦恼?你是否在各种NoSql间纠结不定,到底该选用那种最好?今天的你就是昨天的我,这也是写这篇文章的初衷. 这篇文章是我好几 ...
- ES 27 - Elasticsearch脚本的使用实践
目录 1 关于脚本 2 脚本使用的最佳实践 2.1 创建脚本并存储 2.2 脚本的缓存 2.3 Script Field - 脚本字段 本文以 ES 6.6.0 版本为例进行演示. 1 关于脚本 ES ...
- GOF23-工厂模式
1.什么是工厂模式 就是实现创建者与调用者分离,工厂模式的核心(灵魂)其实就是:分工. 2.工厂模式有哪些 简单工场模式(静态工厂) 简单工厂模式也叫静态工厂模式,就是工厂类一般使用静态方法,通过 ...