結果

問題 No.187 中華風 (Hard)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-17 18:56:03
言語 D
(dmd 2.107.1)
結果
WA  
実行時間 -
コード長 3,618 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 111,316 KB
実行使用メモリ 6,968 KB
最終ジャッジ日時 2023-09-03 22:13:33
合計ジャッジ時間 6,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
4,380 KB
testcase_01 AC 38 ms
4,376 KB
testcase_02 AC 128 ms
6,932 KB
testcase_03 AC 125 ms
6,900 KB
testcase_04 AC 263 ms
6,900 KB
testcase_05 AC 256 ms
6,936 KB
testcase_06 AC 259 ms
6,948 KB
testcase_07 AC 261 ms
6,932 KB
testcase_08 AC 419 ms
6,916 KB
testcase_09 AC 417 ms
6,912 KB
testcase_10 AC 420 ms
6,948 KB
testcase_11 AC 259 ms
6,932 KB
testcase_12 AC 263 ms
6,908 KB
testcase_13 AC 275 ms
4,380 KB
testcase_14 AC 229 ms
4,376 KB
testcase_15 AC 128 ms
6,940 KB
testcase_16 AC 131 ms
6,964 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 33 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 217 ms
6,964 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 262 ms
6,968 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m must be positive");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b;
    a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) {
        y = -y;
      }
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a;
    b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) {
        x = -x;
      }
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

// Find the smallest x (>= 0) s.t. x == a[i] (mod m[i]), mod m0
//   x = y[0] + m[0] y[1] + m[0] m[1] y[2] + ... + m[0] ... m[n - 1] y[n - 1]
long garner(long[] a, long[] m, long m0)
in {
  foreach (i; 0 .. m.length) {
    assert(0 < m[i] && m[i] <= 0x7fffffff, "garner: 0 < m < 2^31 must hold");
  }
}
do {
  long t;
  auto y = new long[m.length];
  foreach (i; 0 .. m.length) {
    y[i] = a[i] % m[i];
    t = 1;
    foreach (j; 0 .. i) {
      (y[i] -= t * y[j]) %= m[i];
      (t *= m[j]) %= m[i];
    }
    if (((y[i] *= modInv(t, m[i])) %= m[i]) < 0) {
      y[i] += m[i];
    }
  }
  long x = 0;
  t = 1;
  foreach (i; 0 .. m.length) {
    (x += t * y[i]) %= m0;
    (t *= m[i]) %= m0;
  }
  return x;
}


immutable MO = 10L^^9 + 7;

int N;
long[] X, Y;

long solve() {
  long[] ps;
  foreach (i; 0 .. N) {
    long y = Y[i];
    for (long d = 2; d * d <= y; ++d) {
      if (y % d == 0) {
        ps ~= d;
        for (; (y /= d) % d == 0; ) {}
      }
    }
    if (y > 1) {
      ps ~= y;
    }
  }
  ps = ps.sort.uniq.array;
  debug {
    writeln("ps = ", ps);
  }
  foreach (p; ps) {
    auto pks = new long[N];
    pks[] = 1;
    foreach (i; 0 .. N) {
      long y = Y[i];
      for (; y % p == 0; y /= p) {
        pks[i] *= p;
      }
    }
    const i0 = pks.maxIndex;
    foreach (i; 0 .. N) {
      if (X[i] % pks[i] != X[i0] % pks[i]) {
        return -1;
      }
      if (i != i0) {
        Y[i] /= pks[i];
        X[i] %= Y[i];
      }
    }
  }
  debug {
    writeln("X = ", X);
    writeln("Y = ", Y);
  }
  const res = garner(X, Y, MO);
  if (res == 0) {
    long ans = 1;
    foreach (i; 0 .. N) {
      (ans *= Y[i]) %= MO;
    }
    return ans;
  } else {
    return res;
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      X = new long[N];
      Y = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong();
        Y[i] = readLong();
      }
      const ans = solve();
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0