北航本科密码学实验一

32 篇文章 1 订阅
13 篇文章 0 订阅
      
# while True:
    # try:
        
    # except:             
        # break

# 厄拉多塞筛法O(nloglogn)      
# N = 1000000
# isprime = [0 for i in range(N)]
# prime = [0 for i in range(N)]  
# n = int(input())
# cnt=0
# for i in range(2,N):
    # if not isprime[i]:
        # prime[cnt]=i
        # cnt+=1
    # for j in range(cnt):
        # if prime[j]*i>=N:
            # break
        # isprime[i*prime[j]]=1
        # if i%prime[j]==0:
            # break
# for i in range(n+1):
    # print(prime[i],end=" ")

#欧几里得算法O(log(N))
# def gcd1(a, b):
    # while b != 0:
        # r = a % b
        # a = b
        # b = r
    # return a    
# def gcd(a,b):
    # if b==0:
        # return a
    # r=a%b
    # return gcd(b,r)
# n,m = map(int,input().split())
# print(gcd(n,m))

# #扩展欧几里得算法  
# 当 b=0 时,gcd(a,b)=a,此时 x=1 , y=0

       #   当 b!=0 时,

#          设 ax1+by1=gcd(a,b)=gcd(b,a%b)=bx2+(a%b)y2

       #   又因 a%b=a-a/b*b

       #   则 ax1+by1=bx2+(a-a/b*b)y2

#     ax1+by1=bx2+ay2-a/b*by2

#     ax1+by1=ay2+bx2-b*a/b*y2

#     ax1+by1=ay2+b(x2-a/b*y2)

#     解得 x1=y2 , y1=x2-a/b*y2

#     因为当 b=0 时存在 x , y 为最后一组解

#     而每一组的解可根据后一组得到

#     所以第一组的解 x , y 必然存在  

def exgcd(a, b):
    # a*xi + b*yi = ri
    if b == 0:
        return (1, 0, a)
    (x, y, r) = exgcd(b, a%b)
    tmp = x
    x = y
    y = tmp - (a//b) * y
    return (x, y, r)

# n,m = map(int,input().split())
# x,y,r = exgcd(n,m)
# print("x=%d, y=%d, r=%d" %(x,y,r))
#中国剩余定理
# def China(n):
    # M=1
    # x=0
    # for i in range(n):
        # M*=m[i]
    # for i in range(n):
        # w=M/m[i]
        # x,y,r=exgcd(m[i],w)
        # x=(x+y*w*a[i])%M
    # return (x+M)%M; 
# m = [0 for i in range(15)]
# a = [0 for i in range(15)]
# n= int(input())
# for i in range(n):
    # m[i],a[i] = map(int,input().split())
# print(China(n))


c++
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
ll m[100005],r[100005];
ll exgcd(ll a,ll b,ll &x,ll &y){
    if(b==0){
        x=1;y=0;
        return a;
    }
    else {
        ll r=exgcd(b,a%b,y,x);
        y-=x*(a//b);
        return r;
    }
}
ll excrt(ll *m,ll *r,int n){  //求最小正整数解
    ll M=m[1],R=r[1],x,y,d;
    for(int i=2;i<=n;i++){
        ll d=exgcd(M,m[i],x,y);
        if((r[i]-R)%d!=0)return -1;
        x=((r[i]-R)/d*x)%(m[i]/d);
        R+=M*x;
        M=M/d*m[i];  //没m[i]*m[i+1]/gcd(m[i],m[i+1]) 求lcm
        R%=M;
    }
    return R>0?R:R+M;
}
int main()
{
    int i,j,k,t,n;
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++){
            scanf("%d%d",&m[i],&r[i]);
        }
        printf("%lld\n",excrt(m,r,n));
    }
    return 0;
}
2
8 7
11 9

# #模幂O(n)
# sum = 1
# mod = 1000000007
# a,b = map(int,input().split())
# for i in range(b):
    # sum = (sum*a)%mod
# print(sum%mod)

# #快速幂O(logn)
# def qmod(a,b,mod):
    # sum = 1
    # while b>0:
        # if b%2==1:
            # sum =(sum*a)%mod
            # b-=1
        # elif b%2==0:
            # b=b//2
            # a = (a*a)%mod
    # return sum%mod
# mod = 1000000007
# x,y = map(int,input().split())
# c = qmod(x,y,mod)
# print(c)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值