結果

問題 No.186 中華風 (Easy)
ユーザー teeteew76teeteew76
提出日時 2021-02-28 14:51:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,967 bytes
コンパイル時間 1,885 ms
コンパイル使用メモリ 200,704 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-10 15:59:01
合計ジャッジ時間 2,676 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
    for(int i = 0; i < (int)v.size(); i++) {
        os << v[i] << (i + 1 != (int)v.size() ? " " : "");
    }
    return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
    for(T &in : v) is >> in;
    return is;
}

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) res += ", ";
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef PLOKI_LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// calculate ap + bq = gcd(a, b)
// return d = gcd(a, b)
ll extgcd(ll a, ll b, ll& p, ll& q) {
    if(b == 0) {
        p = 1;
        q = 0;
        return a;
    }
    ll d = extgcd(b, a % b, q, p);
    q -= a / b * p;
    return d;
}
// solve the equation
// * x == r1 (mod. m1)
// * x == r2 (mod. m2)
// let return val be (r, m),
// the solution can be written as 
// (r, m) == (0, -1) if not exist
pair<ll, ll> crt(ll r1, ll m1, ll r2, ll m2) {
    ll p, q;
    ll d = extgcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
    if((r2 - r1) % d != 0) return {0, -1};
    ll m = m1 * (m2 / d); // lcm of (m1, m2)
    ll tmp = (r2 - r1) / d * p % (m2 / d);
    ll r = r1 + m1 * tmp;
    r = (r % m + m) % m;
    return {r, m};
}

int main() {
    vector<int> x(3), y(3);
    for (int i = 0; i < 3; ++i) {
        cin >> x[i] >> y[i];
    }

    auto [xt, yt] = crt(x[0], y[0], x[1], y[1]);
    if(xt == 0 && yt == -1) {
        cout << -1 << endl;
        return 0;
    }
    auto [ax, ay] = crt(xt, yt, x[2], y[2]);
    if(ax == 0 && ay == -1) {
        cout << -1 << endl;
        return 0;
    }
    debug(ax, ay);
    if(ax <= 0) ax += ay;
    cout << ax << endl;
}
0