結果

問題 No.186 中華風 (Easy)
ユーザー tubo28tubo28
提出日時 2015-10-23 18:49:25
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,802 bytes
コンパイル時間 475 ms
コンパイル使用メモリ 68,280 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-29 17:44:03
合計ジャッジ時間 1,439 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <tuple>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef __int128_t lll;

#define abs(a) (a > 0 ? a : -a)

lll extgcd(lll a, lll b, lll &x, lll &y) {
  for (lll u = y = 1, v = x = 0; a; ) {
    lll q = b / a;
    swap(x -= q * u, u);
    swap(y -= q * v, v);
    swap(b -= q * a, a);
  }
  return b;
}

lll gcd(lll a, lll b){
  return b ? gcd(b,a%b) : a;
}

lll lcm(lll a, lll b){
  return a/gcd(a,b)*a;
}

lll modinv(lll x, lll m) {
  lll s, t;
  extgcd(x, m, s, t);
  return (m+s) % m;
}

lll mul(lll a, lll b, lll m){
  if(a < 0) a = m - abs(a)%m;
  else a %= m;
  if(b < 0) b = m - abs(b)%m;
  else b %= m;
  return a*b%m;
}

// x, mod
pair<lll,lll> crt(lll a1, lll m1, lll a2, lll m2){
  lll k1,k2;
  lll g = extgcd(m1,m2,k1,k2);
  if(abs(a1-a2)%g != 0){
    return make_pair(-1,-1);
  } else {
    lll l = m1 / g * m2;
    k1 *= (a2-a1)/g;
    k2 *= (a2-a1)/(-g);
    assert((a2-a1)%g == 0);
    assert((a2-a1)%(-g) == 0);
    assert(m1*k1 - m2*k2 == a2-a1);
    printf(" %lld %lld %lld %lld\n", (ll)m1,(ll)k1,(ll)m2,(ll)k2);
    lll x1 = (a1 + mul(k1,m1,l)) % l;
    lll x2 = (a2 + mul(k2,m2,l)) % l;
    printf(" x1=%lld x2=%lld\n", (ll)x1,(ll)x2);
    assert(x1 == x2);
    return make_pair(x1,l);
  }
}

int n;
lll x[1010], y[1010];
const lll mod = 1000000007;

pair<lll,lll> solve(){
  lll m = 1, ans = 0;
  for(int i = 0; i < 3; i++){
    tie(ans,m) = crt(ans, m, x[i], y[i]);
    if(ans == -1) return make_pair(-1,-1);
  }
  return make_pair(ans,m);
}

int main(){
  while(1){
    for(int i = 0; i < 3; i++){
      ll xx,yy;
      if(!(cin >> xx >> yy)) exit(0);
      x[i] = xx, y[i] = yy;
    }
    lll ans,m;
    tie(ans,m) = solve();
    if(ans == 0) ans = m;
    cout << (ll)ans << endl;
  }
}
0