結果

問題 No.377 背景パターン
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-18 19:30:21
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 506 ms / 5,000 ms
コード長 2,955 bytes
コンパイル時間 896 ms
コンパイル使用メモリ 117,212 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 22:23:23
合計ジャッジ時間 3,093 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 485 ms
4,384 KB
testcase_17 AC 506 ms
4,380 KB
testcase_18 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, 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 > 0 must hold");
}
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;
  }
}


immutable MO = 10L^^9 + 7;

long power(long a, long e) {
  long x = a % MO, y = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      (y *= x) %= MO;
    }
    (x *= x) %= MO;
  }
  return y;
}

Tuple!(long, long)[] getDivisors(long n) {
  Tuple!(long, long)[] ds;
  for (long d = 1; d * d <= n; ++d) {
    if (n % d == 0) {
      ds ~= tuple(d, 0L);
      if (d != n / d) {
        ds ~= tuple(n / d, 0L);
      }
    }
  }
  ds.sort;
  foreach_reverse (i; 0 .. ds.length) {
    ds[i][1] = n / ds[i][0];
    foreach_reverse (j; i + 1 .. ds.length) {
      if (ds[j][0] % ds[i][0] == 0) {
        ds[i][1] -= ds[j][1];
      }
    }
  }
  return ds;
}

long M, N, K;

void main() {
  try {
    for (; ; ) {
      M = readLong();
      N = readLong();
      K = readLong();
      const dsM = getDivisors(M);
      const dsN = getDivisors(N);
      debug {
        writeln("M: ", dsM);
        writeln("N: ", dsN);
      }
      long ans;
      foreach (x; dsM) foreach (y; dsN) {
        /*
          (M, 0), (0, N), (a, b)
          det = gcd(M N, M b, a N)
        */
        long prod = power(K, gcd(M * y[0], x[0] * N));
        (prod *= x[1]) %= MO;
        (prod *= y[1]) %= MO;
        (ans += prod) %= MO;
      }
      (ans *= modInv(M * N, MO)) %= MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0