結果

問題 No.613 Solitude by the window
コンテスト
ユーザー zimpha
提出日時 2017-12-13 00:12:40
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 936 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 397 ms
コンパイル使用メモリ 67,712 KB
実行使用メモリ 137,856 KB
最終ジャッジ日時 2026-06-06 19:52:05
合計ジャッジ時間 11,612 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 WA * 14 RE * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:44:16: warning: 'extra' may be used uninitialized [-Wmaybe-uninitialized]
   44 |   n %= (target - extra);
      |        ~~~~~~~~^~~~~~~~
main.cpp:21:20: note: 'extra' was declared here
   21 |   int64 x = 2 % m, extra, target = -1, end;
      |                    ^~~~~

ソースコード

diff #
raw source code

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

using int64 = long long;

int main() {
  int64 n, m;
  scanf("%lld%lld", &n, &m);
  if (m == 1000000007) {
    n %= 250000001;
    int64 x = 2;
    for (int i = 0; i < n; ++i) {
      x = (x * x + 4 * x) % m;
    }
    printf("%lld\n", x);
    return 0;
  }
  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;
  }
  n -= extra;
  n %= (target - extra);
  if (n == 0) n += (target - extra);
  for (int i = 0; i < n; ++i) {
    x = (x * x + x * 4) % m;
  }
  printf("%lld\n", x);
  return 0;
}
0