結果

問題 No.2207 pCr検査
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-02-03 21:27:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,238 ms / 3,000 ms
コード長 4,268 bytes
コンパイル時間 859 ms
コンパイル使用メモリ 121,724 KB
実行使用メモリ 86,004 KB
最終ジャッジ日時 2024-06-22 17:23:18
合計ジャッジ時間 23,264 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 789 ms
81,992 KB
testcase_03 AC 437 ms
47,644 KB
testcase_04 AC 530 ms
61,856 KB
testcase_05 AC 818 ms
78,012 KB
testcase_06 AC 520 ms
72,464 KB
testcase_07 AC 355 ms
46,696 KB
testcase_08 AC 221 ms
27,648 KB
testcase_09 AC 634 ms
61,696 KB
testcase_10 AC 257 ms
45,616 KB
testcase_11 AC 595 ms
60,032 KB
testcase_12 AC 1,007 ms
69,632 KB
testcase_13 AC 566 ms
44,308 KB
testcase_14 AC 130 ms
13,184 KB
testcase_15 AC 1,134 ms
80,036 KB
testcase_16 AC 493 ms
38,444 KB
testcase_17 AC 942 ms
64,128 KB
testcase_18 AC 625 ms
50,784 KB
testcase_19 AC 332 ms
27,264 KB
testcase_20 AC 482 ms
40,704 KB
testcase_21 AC 1,238 ms
82,640 KB
testcase_22 AC 387 ms
20,188 KB
testcase_23 AC 391 ms
20,276 KB
testcase_24 AC 147 ms
16,696 KB
testcase_25 AC 485 ms
39,196 KB
testcase_26 AC 426 ms
34,688 KB
testcase_27 AC 888 ms
66,040 KB
testcase_28 AC 1,034 ms
86,004 KB
testcase_29 AC 1,103 ms
85,972 KB
testcase_30 AC 1,083 ms
85,968 KB
testcase_31 AC 1,056 ms
85,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, 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; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

struct ModLong(ulong M_) {
  import std.conv : to;
  alias M = M_;
  enum INV_M = 1.0L / M;
  ulong x;
  this(ModLong a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = cast(ulong)(x_ % M); }
  this(int x_) { x = ((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_; }
  this(long x_) { x = cast(ulong)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModLong opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModLong(a); }
  ref ModLong opOpAssign(string op, T)(T a) {
    static if (is(T == ModLong)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") {
        // https://github.com/kth-competitive-programming/kactl/blob/main/doc/modmul-proof.pdf
        // This works at least for M < 2^62, assuming the usual >= 80-bit real.
        const long y = x * a.x - M * cast(ulong)(INV_M * x * a.x);
        x = (y < 0) ? (y + M) : (y >= cast(long)(M)) ? (y - M) : y;
      }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModLong b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModLong(a)");
    }
  }
  ModLong inv() const {
    ulong a = M, b = x; long y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(long)(q) * z; y = z; z = w; }
    assert(a == 1); return ModLong(y);
  }
  ModLong opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModLong a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModLong opBinary(string op, T)(T a) const { return mixin("ModLong(this) " ~ op ~ "= a"); }
  ModLong opBinaryRight(string op, T)(T a) const { return mixin("ModLong(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

enum MO = 1234567890123456817L;
alias Mint = ModLong!MO;


int K;
int[] P, E;

bool solve() {
  if (E[K - 1] != 1) {
    return false;
  }
  const N = P[K - 1];
  
  Mint tar = 1;
  foreach (k; 0 .. K) {
    tar *= Mint(P[k])^^E[k];
  }
  
  auto inv = new Mint[N + 1];
  inv[1] = 1;
  foreach (i; 2 .. N + 1) {
    inv[i] = -((Mint.M / i) * inv[cast(size_t)(Mint.M % i)]);
  }
  Mint bn = 1;
  for (int i = 0; ; ++i) {
    if (bn == tar) {
      writeln(N, " ", i);
      return true;
    }
    if (i == N) break;
    bn *= (N - i);
    bn *= inv[1 + i];
  }
  return false;
}

void main() {
  try {
    for (; ; ) {
      K = readInt;
      P = new int[K];
      E = new int[K];
      foreach (k; 0 .. K) {
        P[k] = readInt;
        E[k] = readInt;
      }
      
      const res = solve();
      if (!res) {
        writeln("-1 -1");
      }
    }
  } catch (EOFException e) {
  }
}
0