結果

問題 No.186 中華風 (Easy)
ユーザー codershifthcodershifth
提出日時 2016-01-13 22:46:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,151 bytes
コンパイル時間 1,354 ms
コンパイル使用メモリ 150,984 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-27 00:48:31
合計ジャッジ時間 2,434 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

template<typename T>
T gcd(T a, T b)
{
        if ( std::abs(a) < std::abs(b) )
            std::swap(a,b);
        if ( b == 0 )
            return a;
        return gcd(b, a%b);
}
template<typename T>
T extgcd(T a, T b, T &x, T &y)
{
    T d = a;
    if (b != 0)
    {
        d = extgcd(b, a%b, y, x);
        y -= (a/b)*x;
    }
    else
    {
        x=1; y=0;
    }
    return d;
}
//  mod M の逆元を求める
template<typename T>
T mod_inverse(T a, T M)
{
        T x, y;
        T d = extgcd(a, M, x, y);
        assert(d == 1);
        return (M + x%M) % M;
}
template <typename T>
std::pair<T,T> linear_congruence(
    const std::vector<T> &A,
    const std::vector<T> &B,
    const std::vector<T> &M)
{
        T x = 0, m = 1;
        for (int i = 0; i < A.size(); ++i)
        {
            T a = A[i] * m;
            T b = B[i] - A[i] * x;
            T d = gcd(M[i], a);
            if (b % d) // solution does not exist.
                return std::make_pair(0, -1);
            T md = M[i]/d;
            T t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
            x = x + m * t;
            m *= M[i] / d;
        }
        return std::make_pair(x % m, m);
}
class ChiniseStyleEasy
{
public:
    void solve(void)
    {
        vector<ll> x(3);
        vector<ll> y(3);

        REP(i, 3)
            cin>>x[i]>>y[i];
        vector<ll> a(3,1);
        ll z,m;
        tie(z,m) = linear_congruence(a,x,y);

        if (m < 0)
        {
            cout<<-1<<endl;
            return;
        }
        ll Y = max({y[0],y[1],y[2]});

        if ( z < 0 )
            z += (-z/m+1)*m;
        ll X = Y/m*m + z;
        if ( X == 0 )
            cout<<m<<endl;
        else
            cout<<X<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new ChiniseStyleEasy();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0