結果

問題 No.1889 K Consecutive Ks (Hard)
ユーザー miscalcmiscalc
提出日時 2021-10-03 14:12:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 870 ms / 6,000 ms
コード長 1,539 bytes
コンパイル時間 3,549 ms
コンパイル使用メモリ 236,576 KB
実行使用メモリ 19,532 KB
最終ジャッジ日時 2024-04-15 00:50:12
合計ジャッジ時間 15,270 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 864 ms
19,420 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 828 ms
18,920 KB
testcase_10 AC 839 ms
19,196 KB
testcase_11 AC 169 ms
7,628 KB
testcase_12 AC 377 ms
11,352 KB
testcase_13 AC 212 ms
8,636 KB
testcase_14 AC 371 ms
11,648 KB
testcase_15 AC 455 ms
13,148 KB
testcase_16 AC 807 ms
19,000 KB
testcase_17 AC 183 ms
7,992 KB
testcase_18 AC 793 ms
18,956 KB
testcase_19 AC 825 ms
19,000 KB
testcase_20 AC 838 ms
19,276 KB
testcase_21 AC 836 ms
19,532 KB
testcase_22 AC 846 ms
19,288 KB
testcase_23 AC 418 ms
12,412 KB
testcase_24 AC 870 ms
19,280 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 <= N; i++)
  {
    for (int d = 1; d * d <= i; d++)
    {
      if (i % d == 0)
      {
        if (d != 1 && d <= M)
          D.at(i - 1)++;
        if (i / d != d && i / d <= M)
          D.at(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