結果

問題 No.186 中華風 (Easy)
ユーザー kappybarkappybar
提出日時 2020-06-03 15:41:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 1,521 ms
コンパイル使用メモリ 167,512 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 01:00:03
合計ジャッジ時間 2,441 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

inline long long mod(long long x,long long m){
    return (x % m + m) % m;
}

long long gcd(long long i,long long j){
    if(j == 0) return i;
    return gcd(j,i%j);
}

//a*x + b*y =gcd(a,b)を満たすx、yをもとめる
pair<long long,long long> extgcd(long long a,long long b){
    if(b == 0) return {1,0};
    pair<long long,long long> res = extgcd(b,a%b);
    long long x = res.first;
    long long y = res.second;
    return {y,x - (a/b) * y};
}

pair<long long,long long> CRT(long long b1,long long m1,long long b2,long long m2){
    pair<long long,long long> pq = extgcd(m1,m2);
    long long p = pq.first;
    long long q = pq.second;
    long long d = gcd(m1,m2);
    if((b2 - b1) % d != 0) return make_pair(0,-1);
    long long m =  m1 * (m2 / d);          //lcm(m1,m2)
    long long tmp = ((b2 - b1) / d * p) % (m2/d);
    long long r = mod(b1 + m1 * tmp, m);
    return make_pair(r,m);
}

int main(){
    ll X,Y;
    cin >> X >> Y;
    rep(i,2){
        ll x,y;
        cin >> x >> y;
        pll ans = CRT(X,Y,x,y);
        if(ans.second == -1){
            cout << -1 << endl;
            return 0;
        }
        X = ans.first;
        Y = ans.second;
        //cout <<  X  << " " << Y << endl;
    }
    if(X ==0){
        cout << Y << endl;
    }else{
        cout << X << endl;
    }
    return 0;
}
0