結果

問題 No.187 中華風 (Hard)
ユーザー te-shte-sh
提出日時 2017-05-09 12:40:47
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 910 ms / 3,000 ms
コード長 1,167 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 162,220 KB
実行使用メモリ 8,076 KB
最終ジャッジ日時 2023-09-03 13:14:03
合計ジャッジ時間 12,074 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 57 ms
8,052 KB
testcase_03 AC 55 ms
8,016 KB
testcase_04 AC 846 ms
8,040 KB
testcase_05 AC 829 ms
8,056 KB
testcase_06 AC 835 ms
8,000 KB
testcase_07 AC 837 ms
8,004 KB
testcase_08 AC 904 ms
8,008 KB
testcase_09 AC 910 ms
8,004 KB
testcase_10 AC 904 ms
8,064 KB
testcase_11 AC 892 ms
8,048 KB
testcase_12 AC 844 ms
8,052 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 AC 44 ms
8,036 KB
testcase_16 AC 45 ms
8,076 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 600 ms
8,044 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 844 ms
8,036 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags
import std.bigint;    // BigInt

const p = 10^^9 + 7;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto xi = new BigInt[](n), yi = new BigInt[](n);
  foreach (ref x, ref y; lockstep(xi, yi)) {
    auto rd = readln.split.to!(BigInt[]);
    x = rd[0]; y = rd[1];
  }

  auto z = xi[0], l = yi[0];

  foreach (i; 1..n) {
    auto r = calc(z, l, xi[i], yi[i]); z = r[0]; l = r[1];
    if (l == -1) {
      writeln(-1);
      return;
    }
  }

  writeln((z == 0 ? l : z) % p);
}

auto calc(T)(T x1, T y1, T x2, T y2)
{
  auto g = euclid(y1, y2);
  if (x1 % g != x2 % g) return Tuple!(T, T)(BigInt(-1), BigInt(-1));

  T m, n;
  exEuclid(y1, y2, m, n);
  m *= (x2 - x1) / g;

  auto l = y1 / g * y2;

  return Tuple!(T, T)(((m * y1 + x1) % l + l) % l, l);
}

pure T euclid(T)(T a, T b)
{
  if (a < b) return euclid(b, a);
  auto c = a % b;
  return c == 0 ? b : euclid(b, c);
}

pure T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b != 0) {
    g = exEuclid(b, a % b, y, x);
    y -= a / b * x;
  }
  return g;
}
0