結果

問題 No.2117 中国剰余定理入門
ユーザー wanuiwanui
提出日時 2022-11-04 23:09:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,440 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 199,332 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 01:39:13
合計ジャッジ時間 3,202 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }
// clang-format on

ll extgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}
ll gcdf(ll a, ll b) {
    if (b == 0) {
        return a;
    }
    return gcdf(b, a % b);
}

int main() {
    ioinit();
    ll b0, c0, b1, c1;
    cin >> b0 >> c0 >> b1 >> c1;
    c0 %= b0;
    if (c0 < 0) c0 += b0;
    c1 %= b1;
    if (c1 < 0) c1 += b1;

    ll g = gcdf(b0, b1);
    if (c0 > c1) {
        swap(b0, b1);
        swap(c0, c1);
    }
    ll dif = c1 - c0;
    if (dif % g != 0) {
        print("NaN");
        return 0;
    }
    ll x, y;
    extgcd(b0, b1, x, y);
    y = -y;

    x *= dif / g;
    y *= dif / g;

    // {
    //     ll p = 1e9;
    //     x -= p * b1 / g;
    //     y -= p * b0 / g;
    // }
    {
        ll s = b0 * x + c0;
        ll t = b1 * y + c1;

        ll lcm = b1 * b0 / g;
        // s + lcm*p >= 0
        // t + lcm*p >= 0
        ll p = 0;
        if (s < 0) {
            p = max(p, (lcm - 1 - s) / lcm);
        }
        if (t < 0) {
            p = max(p, (lcm - 1 - t) / lcm);
        }
        x += p * b1 / g;
        y += p * b0 / g;
        //        print(p, x, y);
    }

    {
        ll s = b0 * x + c0;
        ll t = b1 * y + c1;
        assert(s == t);
        assert(s >= 0);
        print(s, x, y);
    }

    return 0;
}
0