题目:

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. EF Core的Code First 基础

    一.创建实体类与映射类 通过NuGet引用Microsoft.EntityFrameworkCore 1.创建实体类 Code First可以通过为实体类字段添加相应特性,来创建对应的字段类型等,举例 ...

  2. Git-命令行-使用 git stash 暂存代码

    为什么我们需要它不得不说,在知道这个命令的时,以及之后的使用中,我都超级热爱这个命令,因为它真的太好用了. 给大家说一下我使用这个命令的场景: 此时我在 feature_666 分支,非常聚精会神加持 ...

  3. Python开发异步任务Celery的使用教程!

    1. 生产者消费者设计模式 最常用的解耦方式之一,寻找中间人(broker)搭桥,保证两个业务没有直接关联.我们称这一解耦方式为:生产者消费者设计模式 2.中间人broker 示例:此处演示Redis ...

  4. JavaScript的event对象

    JavaScript的event对象中 event.target指代的是:触发事件的元素 event.currentTarget指代的是:事件绑定的元素 <!DOCTYPE html> & ...

  5. Danjgo学习笔记(一)

    ## 创建项目: 1. 通过命令行的方式:首先要进入到安装了django的虚拟环境中.然后执行命令: ``` django-admin startproject [项目的名称] ``` 这样就可以在当 ...

  6. (十二)c#Winform自定义控件-分页控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. zuul 路由网关 微服务架构系统中

    在微服务架构中,基本包含以下常见的组件.服务注册与发现.服务消费.负载均衡.断路器.只能路由.配置管理等.一个简单的微服务架构系统如下 一.Zuul简介 Zuul的主要功能是路由转发和过滤器.路由功能 ...

  8. Redis----NoSql数据库笔记

    介绍:Redis 是一个开源的使用 ANSI C 语言编写.遵守 BSD 协议.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的 API的非关系型数据库. 传统数据 ...

  9. idea打包报错

    There is insufficient memory for the Java Runtime Environment to continue.# Native memory allocation ...

  10. vue父子组件通信高级用法

    vue项目的一大亮点就是组件化.使用组件可以极大地提高项目中代码的复用率,减少代码量.但是使用组件最大的难点就是父子组件之间的通信. 子通信父 父组件 <template> <div ...