题意分析
显然,$n\equiv a_i\pmod{b_i}$。
因此可以使用 CRT 求解。
AC 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
#define int __int128
typedef long long ll;
void exgcd(int a,int &x,int b,int &y){
if(!b){
x=1;
y=0;
return;
}
int tmp;
exgcd(b,tmp,a%b,x);
y=tmp-a/b*x;
}
int inverse(int a,int p){
int x,y;
exgcd(a,x,p,y);
x%=p;
if(x<0){
x+=p;
}
return x;
}
ll CRT(vector<int>a,vector<int>p){
int L=1;
for(int &i:p){
L*=i;
}
int x=0;
for(int i=0;i<a.size();i++){
int q=L/p[i];
x=(x+1ll*a[i]*q%L*inverse(q,p[i])%L)%L;
}
if(x<0){
x+=L;
}
return x;
}
main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll n;
cin>>n;
vector<int>a(n),p(n);
for(int &i:a){
ll j;
cin>>j;
i=j;
}
for(int &i:p){
ll j;
cin>>j;
i=j;
}
cout<<CRT(a,p)<<'\n';
cout.flush();
/*fclose(stdin);
fclose(stdout);*/
return 0;
}