牛客多校第十场 Han Xin and His Troops(扩展中国剩余)

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

题目描述

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.

输入描述:

The first line of input contains two integers n, m (1≤n≤100,1≤m≤1018)(1 \leq n \leq 100, 1 \leq m \leq 10^{18})(1≤n≤100,1≤m≤1018), the number of clues he told you and the threshold that you think he was probably lying.

The remaining part of the input are n lines, specifying the clues, Each line consists of two integers a, b (0≤b<a<105)(0 \leq b < a < 10^5)(0≤b<a<105), representing a clue that b troops are left over if we count them by a’s.

输出描述:

If there does not exist a non-negative number of troops satisfying all clues, print he was definitely lying\texttt{he was definitely lying}he was definitely lying; otherwise, if the minimum non-negative possible number of troops is greater than m, print he was probably lying\texttt{he was probably lying}he was probably lying; otherwise, print the minimum non-negative number.

示例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

思路:看起来像是excrt的模板题,但是它的范围很大,会爆long long,所以使用__int128,,,一个很玄学的东西,我fo了。

#include<bits/stdc++.h>
using namespace std;
#define ll __int128
ll n,m,a1,a2,m1,m2;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
	if(!b)
	{
		x=1;y=0;return a;
	}
	ll d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
void print(__int128 x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9)
        print(x/10);
    putchar(x%10+'0');
}
int main()
{
	
	scanf("%lld%lld",&n,&m);
	scanf("%lld%lld",&a1,&m1);
	int flag=1;
	for(ll i=0;i<n-1;i++)
	{
		scanf("%lld%lld",&a2,&m2);
		ll k1,k2,d,t;
		d=exgcd(a1,a2,k1,k2);
		if((m2-m1)%d)
		{
			flag=0;break;
		}
		k1*=(m2-m1)/d;
		t=a2/d;
		k1=(k1%t+t)%t;
		m1=a1*k1+m1;
		a1=a1/d*a2;
		if(a1<0)
			a1=-a1;
	}
	ll ans=(m1%a1+a1)%a1;
	if(!flag)puts("he was definitely lying");
    else if(ans>m)puts("he was probably lying");
    else print(ans);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值