結果

問題 No.613 Solitude by the window
ユーザー zimphazimpha
提出日時 2017-12-13 00:57:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,900 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 54,944 KB
実行使用メモリ 123,844 KB
最終ジャッジ日時 2023-08-21 00:01:50
合計ジャッジ時間 6,452 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 43 ms
94,756 KB
testcase_06 AC 48 ms
86,268 KB
testcase_07 AC 620 ms
107,948 KB
testcase_08 AC 493 ms
123,844 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 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:83:15: 警告: ‘extra’ may be used uninitialized [-Wmaybe-uninitialized]
   83 |     n %= (end - extra);
      |          ~~~~~^~~~~~~~
main.cpp:59:22: 備考: ‘extra’ はここで定義されています
   59 |     int64 x = 2 % m, extra, target = -1, end;
      |                      ^~~~~

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <map>

using int64 = long long;

int64 phi(int64 n) {
  int64 r = n;
  for (int64 i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      r = r / i * (i  -1);
      while (n % i == 0) n /= i;;
    }
  }
  if (n > 1) r = r / n * (n - 1);
  return r;
}

int64 pow_mod(int64 a, int64 n, int64 mod) {
  int64 r = 1;
  for (; n; n >>= 1) {
    if (n & 1) r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}

int64 calc(int64 n, int64 mod) {
  // (2 + sqrt{3}) ^ (2^n) % mod
  n = pow_mod(2, n, mod - 1);
  int64 a = 1, b = 0;
  int64 c = 2, d = 1;
  for (; n; n >>= 1) {
    if (n & 1) {
      int64 u = (a * c + 3 * b * d) % mod;
      int64 v = (a * d + b * c) % mod;
      a = u, b = v;
    }
    int64 u = (c * c + d * d * 3) % mod;
    int64 v = 2 * c * d % mod;
    c = u, d = v;
  }
  return a;
}

int main() {
  int64 n, m;
  scanf("%lld%lld", &n, &m);
  if (m == 2) puts("0");
  else if (n <= 1000000) {
    int64 x = 2;
    for (int i = 1; i <= n; ++i) {
      x = (x * x + x * 4) % m;
    }
    printf("%lld\n", x);
  } else if (pow_mod(3, m / 2, m) == m - 1) {
    std::vector<bool> mark(m);
    mark[2 % m] = 1;
    int64 x = 2 % m, extra, target = -1, end;
    for (int i = 1; i <= n; ++i) {
      x = (x * x + 4 * x) % m;
      if (mark[x]) {
        target = x;
        end = i;
        break;
      }
      mark[x] = 1;
    }
    if (target == -1) {
      printf("%lld\n", x);
      return 0;
    }
    x = 2 % m;
    for (int i = 0; i < n; ++i) {
      if (x == target) {
        extra = i;
        break;
      }
      x = (x * x + 4 * x) % m;
    }
    //printf("%lld %lld %lld\n", extra, end, target);
    n -= extra;
    n %= (end - extra);
    for (int i = 0; i < n; ++i) {
      x = (x * x + x * 4) % m;
    }
    printf("%lld\n", x);
  } else {
    int64 r = calc(n, m) * 2 % m + m - 2;
    printf("%lld\n", r % m);
  }
  return 0;
}
0