結果

問題 No.186 中華風 (Easy)
ユーザー te-shte-sh
提出日時 2017-04-18 19:17:55
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 175,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 12:52:40
合計ジャッジ時間 2,437 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 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 2 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,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

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

void main()
{
  auto n = 3;
  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 r1 = calc(xi[0], yi[0], xi[1], yi[1]), z1 = r1[0], l1 = r1[1];
  if (l1 == -1) {
    writeln(-1);
    return;
  }

  auto r2 = calc(z1, l1, xi[2], yi[2]), z2 = r2[0], l2 = r2[1];
  if (l2 == -1) {
    writeln(-1);
    return;
  }

  writeln(z2 == 0 ? l2 : z2);
}

auto calc(BigInt x1, BigInt y1, BigInt x2, BigInt y2)
{
  auto g = gcd(y1.to!long, y2.to!long);
  if (x1 % g != x2 % g) return Tuple!(BigInt, BigInt)(BigInt(-1), BigInt(-1));

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

  auto l = y1 / g * y2;

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

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