結果

問題 No.187 中華風 (Hard)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-17 18:56:03
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,618 bytes
コンパイル時間 1,199 ms
コンパイル使用メモリ 125,344 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-13 02:52:12
合計ジャッジ時間 6,128 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
6,812 KB
testcase_01 AC 36 ms
6,940 KB
testcase_02 AC 128 ms
6,944 KB
testcase_03 AC 123 ms
6,940 KB
testcase_04 AC 260 ms
6,944 KB
testcase_05 AC 251 ms
6,940 KB
testcase_06 AC 250 ms
6,944 KB
testcase_07 AC 256 ms
6,940 KB
testcase_08 AC 406 ms
6,940 KB
testcase_09 AC 402 ms
6,940 KB
testcase_10 AC 408 ms
6,940 KB
testcase_11 AC 254 ms
6,940 KB
testcase_12 AC 259 ms
6,940 KB
testcase_13 AC 263 ms
6,944 KB
testcase_14 AC 221 ms
6,940 KB
testcase_15 AC 127 ms
6,944 KB
testcase_16 AC 131 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 32 ms
6,944 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 211 ms
6,944 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 258 ms
6,940 KB
testcase_23 WA -
testcase_24 AC 2 ms
6,940 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