結果

問題 No.1172 Add Recursive Sequence
ユーザー noshi91noshi91
提出日時 2020-05-09 19:33:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 4,000 ms
コード長 2,445 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 88,776 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-17 06:58:08
合計ジャッジ時間 2,506 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 9 ms
6,940 KB
testcase_11 AC 9 ms
6,944 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 8 ms
6,944 KB
testcase_14 AC 75 ms
12,288 KB
testcase_15 AC 41 ms
9,984 KB
testcase_16 AC 231 ms
12,232 KB
testcase_17 AC 193 ms
9,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdint>

template <std::uint_fast64_t mod> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 v;

  constexpr modint(const u64 x = 0) noexcept : v(x % mod) {}
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    v += rhs.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (v < rhs.v)
      v += mod;
    v -= rhs.v;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    v = v * rhs.v % mod;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = mod - 2;
    while (exp != 0) {
      if (exp % 2 != 0)
        *this *= rhs;
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  using mint = modint<1000000007>;

  int k, n, m;
  std::cin >> k >> n >> m;

  std::vector<mint> a(k), c(k);

  for (auto &e : a) {
    std::cin >> e.v;
  }
  for (auto &e : c) {
    std::cin >> e.v;
  }
  std::reverse(c.begin(), c.end());

  // a_{n + k} まで計算する
  a.resize(k + n, mint(0));
  for (int i = 0; i < n; ++i) {
    mint &t = a[i + k];
    for (int j = 0; j < k; ++j) {
      t += a[i + j] * c[j];
    }
  }

  auto add = std::vector(n + 1, std::vector<int>());
  auto sub = std::vector(n + 1, std::vector<int>());

  for (int i = 0; i < m; ++i) {
    int l, r;
    std::cin >> l >> r;
    add[l].push_back(0);
    sub[r].push_back(r - l);
  }

  auto s = std::vector(k, mint(0));

  for (int i = 0; i < n; ++i) {
    // s を 1 つ進める
    mint t = 0;
    for (int j = 0; j < k; ++j) {
      t += s[j] * c[j];
    }
    s.erase(s.begin());
    s.push_back(t);

    for (int l : add[i]) {
      for (int j = 0; j < k; ++j) {
        s[j] += a[l + j];
      }
    }

    for (int l : sub[i]) {
      for (int j = 0; j < k; ++j) {
        s[j] -= a[l + j];
      }
    }

    std::cout << s[0].v << "\n";
  }
}
0