結果

問題 No.186 中華風 (Easy)
ユーザー te-shte-sh
提出日時 2016-09-13 16:48:40
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,126 bytes
コンパイル時間 1,560 ms
コンパイル使用メモリ 170,116 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 22:05:55
合計ジャッジ時間 2,724 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.regex, std.conv, std.stdio, std.typecons;

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;
}

void main()
{
  auto rd1 = readln.split.map!(a => BigInt(a));
  auto x1 = rd1[0], y1 = rd1[1];
  auto rd2 = readln.split.map!(a => BigInt(a));
  auto x2 = rd2[0], y2 = rd2[1];
  auto rd3 = readln.split.map!(a => BigInt(a));
  auto x3 = rd3[0], y3 = rd3[1];

  BigInt a, b, g, l, r;

  g = exEuclid(y1, y2, a, b);

  if (g > 1 && x1 % g != x2 % g) {
    writeln(-1);
    return;
  }

  l = y1 / g * y2;
  r = y1 / g * x2 * a + y2 / g * x1 * b;
  if (r < 0) r += ((-r / l) + 1) * l;
  r %= l;

  BigInt x4 = r, y4 = l;

  g = exEuclid(y3, y4, a, b);

  if (g > 1 && x3 % g != x4 % g) {
    writeln(-1);
    return;
  }

  l = y3 / g * y4;
  r = y3 / g * x4 * a + y4 / g * x3 * b;
  if (r < 0) r += ((-r / l) + 1) * l;
  r %= l;

  writeln(r == 0 ? l : r);
}
0