Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- created: 2023-02-06-15.59.28
- ***/
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
- #define ll long long
- #define all(we) we.begin(),we.end()
- #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
- #define nl '\n'
- #define ll long long int
- #define pll pair<ll, ll>
- ll egcd(ll a, ll b, ll& x, ll& y){
- if(!b) {y = 0, x = 1; return a;}
- ll g = egcd(b, a % b, y, x);
- y -= ((a / b) * x);
- return g;
- }
- pll GeneralCRT(pll a, pll b){
- if(a.second < b.second) swap(a, b);
- ///a.first=mod,a.second=rem
- ll x, y; egcd(a.second, b.second, x, y);
- ll g = a.second * x + b.second * y;
- ll l = a.second / g * b.second;
- if((b.first - a.first) % g) {return {-1, -1};} // No Solution
- ll c = (b.first - a.first) % b.second;
- c = (c * x) % b.second;
- c = c / g * a.second;
- c += a.first;
- if(c < 0) {c += l;}
- return {c, l};
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- ll n,m,i,j,k,l;
- cin>>n;
- vector<pll>x(n);
- for(i=0;i<n;i++)
- {
- cin>>x[i].second>>x[i].first;
- }
- pll y=x[0];
- for(i=1;i<n;i++)
- {
- y=GeneralCRT(x[i],y);
- }
- cout<<y.first<<nl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement