結果

問題 No.181 A↑↑N mod M
ユーザー te-sh
提出日時 2017-05-02 13:47:16
言語 D
(dmd 2.109.1)
結果
WA  
実行時間 -
コード長 1,151 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 105,412 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-12 19:00:20
合計ジャッジ時間 2,185 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 16 WA * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/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