2019牛客暑期多校训练营(第十场) D: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 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.

中国剩余定理模板题, 要用高精度

M = 0
m = [0] * 110
a = [0] * 110
x = 0
y = 0
def exgcd(a, b):
    global x, y
    if b == 0:
        x = 1
        y = 0
        return a
    else:
        d = exgcd(b, a % b)
        t = x
        x = y
        y = t - (a // b) * y
        return d
def CRT(n):
    global x, y, M
    flag = 0
    for i in  range(1, n):
        d = exgcd(m[0], m[i])
        #print(d)
        u = a[i] - a[0]
        if u % d != 0:
            flag = 1
            break
        x = (x * u % m[i]) // d
        m[i] //= d
        x = (x % m[i] + m[i]) % m[i]
        a[0] += m[0] * x
        m[0] *= m[i]
    # print(flag)
    if flag == 1:
        print("he was definitely lying")
    elif a[0] > M:
        print("he was probably lying")
    else:
        print(a[0])
n, M = map(int, input().split())
for i in range(0, n):
    m[i], a[i] = map(int, input().split())
CRT(n)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值