跳到正文

题解

题解 P1495 【曹冲养猪】

洛必达法则约 2 分钟
原文
文章信息与版本

文章信息

发布
2020年1月15日
收录
2026年6月30日
更新
2026年11月11日
篇幅
约 2 分钟
题目
P1495

版本

  1. v1当前

中国剩余定理模板

欧拉函数求逆元

因数据范围过大,需要高精度

Python
from os import*
n=int(input())
p=[0];mod=[0]
P=1
rng=-1
low=1
def gcd(x,y):
    p=min(x,y);q=max(x,y)
    if q%p==0:
        return p
    return gcd(p,q%p)
def lcm(x,y):
    return x/gcd(x,y)*y
for i in range(1,n+1):
    p.append(0)
    mod.append(0)
    p[i],mod[i]=input().split(' ')
    p[i]=int(p[i]);mod[i]=int(mod[i])
    P*=p[i]
    rng=max(rng,p[i])
    low=lcm(low,p[i])
if n==1:
    print(mod[1])
    exit()
n_odd=[False]
phi=[0,1,1,2]
for i in range(1,rng+1):
    n_odd.append(False)
    phi.append(0)
n_odd[1]=True
for i in range(1,rng+1):
    if not n_odd[i]:
        phi[i]=i-1
    for j in range(2,i+1):
        if n_odd[j]:
            continue
        if i*j>rng:
            break
        n_odd[i*j]=True
        if i%j==0:
            phi[i*j]=j*phi[i]
            break
        else:
            phi[i*j]=phi[i]*phi[j]
ans=0
for i in range(1,n+1):
    delta=int(P/p[i])
    delta*=(int(pow(delta,phi[p[i]]-1))%P)
    delta%=P
    delta*=mod[i]
    delta%=P
    ans+=delta
    ans%=P
ans%=low
print(int(ans));
原文