Advertisement
Saleh127

AUST Carnival 1.0 A / CRT

Feb 6th, 2023
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. /***
  2.  created: 2023-02-06-15.59.28
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define all(we) we.begin(),we.end()
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define nl '\n'
  15. #define ll long long int
  16. #define pll pair<ll, ll>
  17. ll egcd(ll a, ll b, ll& x, ll& y){
  18.     if(!b) {y = 0, x = 1; return a;}
  19.     ll g = egcd(b, a % b, y, x);
  20.     y -= ((a / b) * x);
  21.     return g;
  22. }
  23.  
  24. pll GeneralCRT(pll a, pll b){
  25.     if(a.second < b.second) swap(a, b);
  26.     ///a.first=mod,a.second=rem
  27.     ll x, y; egcd(a.second, b.second, x, y);
  28.     ll g = a.second * x + b.second * y;
  29.     ll l = a.second / g * b.second;
  30.     if((b.first - a.first) % g) {return {-1, -1};} // No Solution
  31.     ll c = (b.first - a.first) % b.second;
  32.     c = (c * x) % b.second;
  33.     c = c / g * a.second;
  34.     c += a.first;
  35.     if(c < 0) {c += l;}
  36.     return {c, l};
  37. }
  38.  
  39.  
  40. int main()
  41. {
  42.     ios_base::sync_with_stdio(0);
  43.     cin.tie(0);
  44.     cout.tie(0);
  45.  
  46.     ll n,m,i,j,k,l;
  47.  
  48.     cin>>n;
  49.  
  50.     vector<pll>x(n);
  51.  
  52.     for(i=0;i<n;i++)
  53.     {
  54.         cin>>x[i].second>>x[i].first;
  55.     }
  56.  
  57.     pll y=x[0];
  58.  
  59.     for(i=1;i<n;i++)
  60.     {
  61.         y=GeneralCRT(x[i],y);
  62.     }
  63.  
  64.     cout<<y.first<<nl;
  65.  
  66.  
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement