結果

問題 No.1889 K Consecutive Ks (Hard)
ユーザー miscalcmiscalc
提出日時 2021-10-03 13:22:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 710 ms / 6,000 ms
コード長 1,408 bytes
コンパイル時間 3,790 ms
コンパイル使用メモリ 237,400 KB
実行使用メモリ 19,416 KB
最終ジャッジ日時 2024-04-15 00:47:30
合計ジャッジ時間 13,239 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 707 ms
19,416 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 688 ms
18,920 KB
testcase_10 AC 699 ms
19,200 KB
testcase_11 AC 157 ms
7,884 KB
testcase_12 AC 323 ms
11,616 KB
testcase_13 AC 173 ms
8,560 KB
testcase_14 AC 337 ms
11,640 KB
testcase_15 AC 364 ms
13,024 KB
testcase_16 AC 705 ms
19,000 KB
testcase_17 AC 157 ms
7,988 KB
testcase_18 AC 688 ms
18,832 KB
testcase_19 AC 696 ms
19,124 KB
testcase_20 AC 699 ms
19,276 KB
testcase_21 AC 700 ms
19,280 KB
testcase_22 AC 705 ms
19,232 KB
testcase_23 AC 347 ms
12,416 KB
testcase_24 AC 710 ms
19,224 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