結果

問題 No.186 中華風 (Easy)
ユーザー mamekinmamekin
提出日時 2015-05-24 13:28:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,070 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 92,504 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:47:09
合計ジャッジ時間 1,820 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

// 最大公約数
long long gcd(long long a, long long b){
    while(b != 0){
        long long tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

// a,b の最大公約数と、ax + by = gcd(a,b) となる x,y を求める
long long extgcd(long long a, long long b, long long &x, long long &y) {
    long long g = a;
    if(b != 0){
        g = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1;
        y = 0;
    }
    return g;
}

// ax ≡ gcd(a, m) (mod m) となる x を求める
// a, m が互いに素ならば、関数値は mod m での a の逆数となる
long long mod_inverse(long long a, long long m)
{
    long long x, y;
    extgcd(a, m, x, y);
    return (x % m + m) % m;
}

// 連立線形合同式 a[i] * x ≡ b[i] (mod m[i]) を解く
pair<long long, long long> ChineseRemainderTheorem(const vector<int>& a, const vector<int>& b, const vector<int>& m)
{
    pair<long long, long long> ret(0, 1);

    for(unsigned i=0; i<a.size(); ++i){
        long long s = a[i] * ret.second;
        long long t = b[i] - a[i] * ret.first;
        long long d = gcd(m[i], s);
        if(t % d != 0)
            return make_pair(-1, -1);
        long long u = t / d * mod_inverse(s / d, m[i] / d) % (m[i] / d);
        ret.first += ret.second * u;
        ret.second *= m[i] / d;
        ret.first = (ret.first % ret.second + ret.second) % ret.second;
    }

    return ret;
}

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

    auto p = ChineseRemainderTheorem(vector<int>(3, 1), x, y);
    if(p.first == 0)
        p.first += p.second;
    cout << p.first << endl;

    return 0;
}
0