結果

問題 No.187 中華風 (Hard)
ユーザー kiyoshi0205kiyoshi0205
提出日時 2020-09-18 16:11:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,943 bytes
コンパイル時間 2,491 ms
コンパイル使用メモリ 183,080 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-04 08:53:21
合計ジャッジ時間 3,989 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using namespace std;
using ll=long long;
#define double long double
using datas=pair<ll,ll>;
using ddatas=pair<double,double>;
using tdata=pair<ll,datas>;
using vec=vector<ll>;
using mat=vector<vec>;
using pvec=vector<datas>;
using pmat=vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) for(i=a;i<(ll)b;++i)
#define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\n"
#define eb emplace_back
#define print(v) cout<<v<<endl
#define printyes cout<<"Yes"<<endl
#define printno cout<<"No"<<endl
#define printYES cout<<"YES"<<endl
#define printNO cout<<"NO"<<endl
#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
const ll mod=1000000007;
// const ll mod=998244353;
const ll inf=1LL<<60;
const double PI = acos(-1);
const double eps = 1e-9;
template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} 
template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} 

void startupcpp(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
}
ll gcd(ll a,ll b){if(!b)return abs(a);return (a%b==0)?abs(b):gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

datas crt(const vec& r,const vec& m) {
  assert(r.size()==m.size());
  int n=int(r.size());
  //Contracts: 0 <=r0 < m0
  ll r0=0,m0=1;
  for(int i=0;i<n;++i){
    assert(1<=m[i]);
    ll r1=r[i]%m[i],m1=m[i];
    if(m0<m1){
      swap(r0,r1);
      swap(m0,m1);
    }
    if(m0%m1==0){
      if(r0%m1!=r1)return {0,0};
      continue;
    }
    ll a=m0%m1;
    if(a<0)a+=m1;
    ll g=m1,im=0;
    //s=g=m1,t=a,m0=im,m1=ims
    //calc inv_gcd(m0,m1)
    if(a){
      ll ims=1;
      while(a){
        ll u=g/a;
        g-=a*u;
        im-=ims*u;
        auto tmp=g;g=a;a=tmp;
        tmp=im;im=ims;ims=tmp;
      }
      if(im<0)im+=m1/g;
    }
    ll u1=(m1/g);
    if((r1-r0)%g)return {0,0};
    ll x=(r1-r0)/g%u1*im%u1;
    r0+=x*m0;
    m0*=u1;
    if(r0<0)r0+=m0;
  }
  return {r0,m0};
}

int main(){
  startupcpp();
  ll i,j,N;
  cin>>N;
  vec r(N),m(N);
  rep(i,N)cin>>r[i]>>m[i];
  tie(i,j)=crt(r,m);
  if(!j)print(-1);
  else if(i)print(i);
  else print(j);
}
0