結果

問題 No.1887 K Consecutive Ks (Easy)
ユーザー miscalcmiscalc
提出日時 2022-03-06 17:23:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,408 bytes
コンパイル時間 3,133 ms
コンパイル使用メモリ 241,080 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 08:39:50
合計ジャッジ時間 4,087 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 7 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;

#include <atcoder/convolution>

vector<mint> dp1, dp2, D, pw;
void init(int N, int M)
{
  D.assign(N + 10, 0);
  for (int i = 2; i <= M; i++)
  {
    for (int k = 1; k * i <= N; k++)
    {
      D.at(k * i - 1)++;
    }
  }

  pw.resize(N + 10);
  pw.at(0) = 0, pw.at(1) = 1, pw.at(2) = M - 2;
  for (int i = 2; i < N; i++)
  {
    pw.at(i + 1) = (M - 1) * pw.at(i);
  }

  dp1.assign(N + 10, 0);
  dp2.resize(N + 10);
  dp2.at(0) = 1;
  for (int i = 0; i < N; i++)
  {
    dp2.at(i + 1) = (M - 1) * dp2.at(i);
  }
}

vector<mint> getvec(const vector<mint> &v, int l, int r)
{
  return {v.begin() + l, v.begin() + r};
}
void onlineconvolution(int l, int r)
{
  if (l + 1 == r)
    return;

  int m = (l + r) / 2;

  onlineconvolution(l, m);

  auto dp20 = getvec(dp2, l, m);
  auto D0 = getvec(D, 0, r - l);
  auto r1 = convolution(dp20, D0);
  for (int i = m; i < r; i++)
  {
    dp1.at(i) += r1.at(i - l);
  }
  auto dp10 = getvec(dp1, l, m);
  auto pw0 = getvec(pw, 0, r - l);
  auto r2 = convolution(dp10, pw0);
  for (int i = m; i < r; i++)
  {
    dp2.at(i) -= r2.at(i - l);
  }

  onlineconvolution(m, r);
}

int main()
{
  int N, M;
  cin >> N >> M;

  init(N, M);

  onlineconvolution(0, N + 1);

  mint ans = ((mint)M).pow(N) - dp2.at(N);
  cout << ans.val() << endl;
}
0