結果

問題 No.187 中華風 (Hard)
ユーザー fumofumofunifumofumofuni
提出日時 2022-09-04 02:55:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 3,000 ms
コード長 2,711 bytes
コンパイル時間 2,268 ms
コンパイル使用メモリ 206,720 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:35:57
合計ジャッジ時間 4,894 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")


#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,1,0,-1,1,-1,1,-1,0};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
bool PreGarner(vector<long long> &b,vector<long long> &m){
  long long res=1;
  for(int i=0;i<b.size();i++){
    for(int j=0;j<i;j++){
      long long g=gcd(m[i],m[j]);
      if((b[i]-b[j])%g!=0)return false;
      m[i]/=g,m[j]/=g;
      long long gi=gcd(m[i],g);
      long long gj=g/gi;
      while(g!=1){//各素べきをiとjのうち指数が大きい方に振り分ける。O(log^2)程度。
        g=gcd(gi,gj);
        gi*=g;gj/=g;
      }
      m[i]*=gi,m[j]*=gj;
      b[i]%=m[i],b[j]%=m[j];
    }
  }
  return true;
}
long long safemod(long long a,long long m){
  long long res=a%m;
  if(res<0)res+=m;
  return res;
}
long long extGCD(long long a,long long b,long long &p,long long &q){
  if(b==0){p=1;q=0;return a;}
  long long d=extGCD(b,a%b,q,p);
  q-=a/b*p;
  return d;
}
long long modinv(long long a,long long m){//aとmが互いに素
  long long x,y;
  extGCD(a,m,x,y);
  return safemod(x,m);
}
pair<long long,long long> Garner(vector<long long> b,vector<long long> m,long long MOD){//{解,lcm(mod)}
  m.emplace_back(MOD);
  long long l=1;
  vector<long long> coeffs(m.size(),1);
  vector<long long> constants(m.size(),0);
  for(int k=0;k<b.size();k++){
    long long t=safemod((b[k]-constants[k])*modinv(coeffs[k],m[k]),m[k]);
    l=l*m[k]%MOD;
    for(int i=k+1;i<m.size();i++){
      constants[i]=(constants[i]+t*coeffs[i])%m[i];
      coeffs[i]=coeffs[i]*m[k]%m[i];
    }
  }
  return make_pair(constants.back(),l);
}
int main(){
  ll n;cin >> n;
  vl b(n),m(n);
  bool alzero=true;
  rep(i,n){
    cin >> b[i] >> m[i];
    if(b[i]!=0)alzero=false;
  }
  auto f=PreGarner(b,m);
  if(f==false){
    cout << -1 << endl;return 0;
  }
  auto [x,l]=Garner(b,m,MOD);
  if(alzero)cout << l << endl;
  else cout << x << endl;
}
0