結果

問題 No.181 A↑↑N mod M
ユーザー te-shte-sh
提出日時 2017-05-02 13:47:16
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,151 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 94,108 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 13:07:49
合計ジャッジ時間 2,895 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 1 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 WA -
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 WA -
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 1 ms
4,380 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 1 ms
4,376 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.d(54): Deprecation: function `std.math.exponential.log` is deprecated - `std.math.exponential.log` called with argument types `(long)` matches both `log(real)`, `log(double)`, and `log(float)`. Cast argument to floating point type instead.
Main.d(57): Deprecation: function `std.math.exponential.log` is deprecated - `std.math.exponential.log` called with argument types `(long)` matches both `log(real)`, `log(double)`, and `log(float)`. Cast argument to floating point type instead.
Main.d(57): Deprecation: function `std.math.exponential.log` is deprecated - `std.math.exponential.log` called with argument types `(long)` matches both `log(real)`, `log(double)`, and `log(float)`. Cast argument to floating point type instead.
Main.d(57): Deprecation: function `std.math.exponential.log` is deprecated - `std.math.exponential.log` called with argument types `(long)` matches both `log(real)`, `log(double)`, and `log(float)`. Cast argument to floating point type instead.
/dmd2/linux/bin64/../../src/phobos/std/numeric.d(2999): Warning: cannot inline function `std.numeric.gcdImpl!ulong.gcdImpl`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions
import std.numeric;   // gcd

void main()
{
  auto rd = readln.split.to!(long[]);
  writeln(calc(rd[0], rd[1], rd[2]));
}

auto calc(long a, long n, long m)
{
  if (n == 0) return 1L;
  if (m == 1) return 0L;

  auto g = gcd(a, m);
  if (g == 1)
    return modPow1(a, calc(a, n - 1, phi(m)), m);

  auto gp = modPow2(g, a, n - 1, m);
  if (gp == 0) return 0;
  return (gp * modPow1(a/g, calc(a, n - 1, phi(m)), m)) % m;
}

auto phi(long n)
{
  auto r = 0L;
  foreach (i; 1..n)
    if (gcd(i, n) == 1) ++r;
  return r;
}

auto modPow1(long a, long n, long m)
{
  if (n == 0) return 1L;
  if (m == 1) return 0L;

  auto r = 1L, s = a;
  while (n > 0) {
    if (n & 1)
      r = (r * s) % m;
    s = (s * s) % m;
    n >>= 1;
  }

  return r;
}

auto modPow2(long g, long a, long n, long m)
{
  if (n == 0) {
    return g;
  } else if (n == 1) {
    if (a * log(g) > m) return 0L;
    return g ^^ a % m;
  } else if (n == 2) {
    if (a * log(a) * log(log(g)) > log(log(m))) return 0L;
    return g ^^ (a ^^ a) % m;
  } else {
    return 0L;
  }
}
0