結果

問題 No.2506 Sum of Weighted Powers
ユーザー 👑 emthrmemthrm
提出日時 2023-03-28 06:36:08
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 263 ms / 2,000 ms
コード長 2,861 bytes
コンパイル時間 2,863 ms
コンパイル使用メモリ 165,096 KB
実行使用メモリ 14,404 KB
最終ジャッジ日時 2023-10-13 18:22:49
合計ジャッジ時間 7,193 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 4 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 4 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 3 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 3 ms
4,352 KB
testcase_23 AC 164 ms
9,700 KB
testcase_24 AC 208 ms
12,580 KB
testcase_25 AC 72 ms
6,028 KB
testcase_26 AC 228 ms
13,276 KB
testcase_27 AC 25 ms
4,352 KB
testcase_28 AC 261 ms
14,244 KB
testcase_29 AC 261 ms
14,340 KB
testcase_30 AC 263 ms
14,160 KB
testcase_31 AC 261 ms
14,404 KB
testcase_32 AC 263 ms
14,148 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 142 ms
5,872 KB
testcase_36 AC 167 ms
6,508 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 1 ms
4,356 KB
testcase_41 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>

#include <atcoder/convolution>
#include <atcoder/modint>

using mint = atcoder::static_modint<943718401>;

mint S(const int n, const mint& x,
       std::vector<mint> a, std::vector<mint> b, std::vector<mint> c) {
  assert(n == std::ssize(a) - 1 &&
         n == std::ssize(b) - 1 &&
         n == std::ssize(c) - 1);

  // ijk \neq 0 のとき A_i B_j C_k x^{ijk} = 0 が成り立つ。
  // ijk = 0 \iff j = 0 または k = 0 に注意する。
  if (x == 0) {
    mint s = 0;
    // j = 0 のとき
    s += std::inner_product(a.begin(), a.end(), b.begin(), mint::raw(0)) * c[0];
    // j > 0 かつ k = 0 のとき
    s += std::inner_product(std::next(a.begin()), a.end(),
                            std::next(c.begin()), mint::raw(0)) * b[0];
    return s;
  }

  // b^{t_i} (i = 0, 1, ..., n) を O(n) 時間で列挙する。
  // ただし `base` を b としている。
  const auto MakeWeights = [n](mint base) -> std::vector<mint> {
    base *= base;
    mint single_exp = 1, double_exp = 1;  // b^{2i}, b^{(i - 1)i}
    std::vector<mint> weights(n + 1, 1);  // b^{t_i}
    for (int i = 0; i < n; ++i) {
      // b^{(i - 1)i} -> b^{i(i + 1)} = b^{(i - 1)i + 2i}
      //                              = b^{(i - 1)i} b^{2i}
      double_exp *= single_exp;
      // b^{2i} -> b^{2(i + 1)} = b^{2i} b^2
      single_exp *= base;
      // b^{(i - 1)i(i + 1) / 3} -> b^{i(i + 1)(i + 2) / 3}
      //                            = b^{(i - 1)i(i + 1) / 3 + i(i + 1)}
      //                            = b^{(i - 1)i(i + 1) / 3} b^{i(i + 1)}
      weights[i + 1] = weights[i] * double_exp;
    }
    return weights;
  };

  // A_i x^{t_i}
  std::ranges::transform(a, MakeWeights(x), a.begin(), std::multiplies<mint>());
  // B_j / x^{t_j}, C_k / x^{t_k}
  const std::vector<mint> weights_inv = MakeWeights(x.inv());
  std::ranges::transform(b, weights_inv, b.begin(), std::multiplies<mint>());
  std::ranges::transform(c, weights_inv, c.begin(), std::multiplies<mint>());

  const std::vector<mint> s = atcoder::convolution(b, c);
  return std::inner_product(a.begin(), a.end(), s.begin(), mint::raw(0));
}

int main() {
  constexpr int kMaxN = 200000;

  const auto GetMint = []() -> mint {
    int x;
    std::cin >> x;
    assert(0 <= x && x < mint::mod());
    return mint::raw(x);
  };

  int n;
  std::cin >> n;
  assert(0 <= n && n <= kMaxN);
  const mint x = GetMint();

  std::vector<mint> a(n + 1), b(n + 1), c(n + 1);
  std::ranges::for_each(a, [GetMint](mint& x) -> void { x = GetMint(); });
  std::ranges::for_each(b, [GetMint](mint& x) -> void { x = GetMint(); });
  std::ranges::for_each(c, [GetMint](mint& x) -> void { x = GetMint(); });

  std::cout << S(n, x, a, b, c).val() << '\n';
  return 0;
}
0