結果
| 問題 |
No.2670 Sum of Products of Interval Lengths
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-03-10 17:11:44 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,679 ms / 2,000 ms |
| コード長 | 17,985 bytes |
| コンパイル時間 | 29,968 ms |
| コンパイル使用メモリ | 364,848 KB |
| 最終ジャッジ日時 | 2025-02-20 03:43:35 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
// いろいろ高速化
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#ifndef HIDDEN_IN_VS // 折りたたみ用
// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS
// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;
// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;
// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定
// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }
// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif // 折りたたみ用
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif
//【任意列の列挙(一括上限指定)】O(m^n)
/*
* 数列 a[0..n) で,∀i, a[i] ∈ [0..m) を満たすもの全てを格納したリストを返す.
*/
vvi enumerate_all_sequences(int n, int m) {
// verify : https://yukicoder.me/problems/no/2670
vvi seqs;
vi seq; // 作成途中の列
function<void()> rf = [&]() {
// 完成していれば記録する.
if (sz(seq) == n) {
seqs.push_back(seq);
return;
}
rep(x, m) {
seq.push_back(x);
rf();
seq.pop_back();
}
};
rf();
return seqs;
}
//【ランレングス符号】O(n)
/*
* a[0..n) をランレングス符号化し,結果を格納したリスト cls を返す.
* cls[i] = {c, l} は前から i 番目の連が l 個の文字 c からなることを表す.
*/
template <class T>
vector<pair<T, int>> run_length_encoding(const vector<T>& a) {
// verify : https://atcoder.jp/contests/arc024/tasks/arc024_2
int n = sz(a);
vector<pair<T, int>> cls;
if (n == 0) return cls;
cls.emplace_back(a[0], 1);
// 今読んでいる文字の種類を記憶する.
T c = a[0];
repi(i, 1, n - 1) {
// 記憶している文字と同じ文字の場合
if (c == a[i]) {
// 列の長さを増やす.
cls.back().second++;
}
// 記憶している文字と異なる文字の場合
else {
// 新しい文字を記憶しておく.
c = a[i];
// 新たな列を追加する.
cls.emplace_back(c, 1);
}
}
return cls;
}
mint naive(int n, ll m) {
auto seqs = enumerate_all_sequences(n, m);
mint res;
repe(a, seqs) {
auto b(a);
rep(i, n) b[i] -= i;
auto rle = run_length_encoding(b);
mint val = 1;
repe(tmp, rle) val *= tmp.second;
res += val;
}
return res;
}
mint TLE(int n, ll m) {
vvvm dp(n + 1, vvm(m, vm(2)));
dp[0][m - 1][1] = 1;
rep(i, n) {
rep(j, m) {
rep(j2, m) {
if (j2 == j + 1) {
dp[i + 1][j2][0] += dp[i][j][0];
dp[i + 1][j2][1] += dp[i][j][0];
dp[i + 1][j2][1] += dp[i][j][1];
}
else {
dp[i + 1][j2][0] += dp[i][j][1];
dp[i + 1][j2][1] += dp[i][j][1];
}
}
}
}
dumpel(dp);
mint res = 0;
rep(j, m) res += dp[n][j][1];
return res;
}
mint TLE2(int n, ll m) {
vvvm dp(n + 1, vvm(m, vm(2)));
dp[0][m - 1][1] = 1;
vm sum(n + 1);
sum[0] = 1;
rep(i, n) {
rep(j, m) {
dp[i + 1][j][0] += sum[i];
dp[i + 1][j][1] += sum[i];
sum[i + 1] += sum[i];
if (j > 0) {
dp[i + 1][j][0] -= dp[i][j - 1][1];
dp[i + 1][j][1] -= dp[i][j - 1][1];
sum[i + 1] -= dp[i][j - 1][1];
dp[i + 1][j][0] += dp[i][j - 1][0];
dp[i + 1][j][1] += dp[i][j - 1][0];
sum[i + 1] += dp[i][j - 1][0];
dp[i + 1][j][1] += dp[i][j - 1][1];
sum[i + 1] += dp[i][j - 1][1];
}
}
}
dumpel(dp);
mint res = 0;
rep(j, m) res += dp[n][j][1];
return res;
}
/*
0: 0 0 0 0 0 0 0 0 0 1
1: 1 1 1 1 1 1 1 1 1 1
2: 5 5 5 6 5 6 5 6 5 6
3: 29 29 29 34 28 34 28 34 28 34
4: 165 165 165 194 160 194 159 193 159 193
5: 939 939 939 1104 910 1104 905 1099 905 1098
5344
*/
mint TLE3(int n, ll m) {
vvvm dp(n + 1, vvm(m + n, vm(2)));
dp[0][m - 1 - 0 + n][1] = 1;
vm sum(n + 1);
sum[0] = 1;
rep(i, n) {
rep(j, m) {
dp[i + 1][j - (i + 1) + n][0] += sum[i];
dp[i + 1][j - (i + 1) + n][1] += sum[i];
sum[i + 1] += sum[i];
if (j > 0) {
dp[i + 1][j - (i + 1) + n][0] += dp[i][j - 1 - i + n][0] - dp[i][j - 1 - i + n][1];
dp[i + 1][j - (i + 1) + n][1] += dp[i][j - 1 - i + n][0];
sum[i + 1] += dp[i][j - 1 - i + n][0];
}
}
}
dump(sum); dumpel(dp);
mint res = 0;
rep(j, m) res += dp[n][j - n + n][1];
return res;
}
/*
0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0
2: 0 0 0 0 0 0 5 5 5 6 5 6 5 6 5 6 0 0 0 0
3: 0 0 0 0 29 29 29 34 28 34 28 34 28 34 0 0 0 0 0 0
4: 0 0 165 165 165 194 160 194 159 193 159 193 0 0 0 0 0 0 0 0
5: 939 939 939 1104 910 1104 905 1099 905 1098 0 0 0 0 0 0 0 0 0 0
5344
*/
mint TLE4(int n, ll m) {
vvvm dp(n + 1, vvm(m + n, vm(2)));
dp[0][m + n - 1][1] = 1;
vm sum(n + 1);
sum[0] = 1;
rep(i, n) {
repi(j, 0, i) { // repi(j, 0, m - 1)
dp[i + 1][j - (i + 1) + n][0] += sum[i];
dp[i + 1][j - (i + 1) + n][1] += sum[i];
sum[i + 1] += sum[i];
if (j > 0) {
dp[i + 1][j - (i + 1) + n][0] += dp[i][j - 1 - i + n][0] - dp[i][j - 1 - i + n][1];
dp[i + 1][j - (i + 1) + n][1] += dp[i][j - 1 - i + n][0];
sum[i + 1] += dp[i][j - 1 - i + n][0];
}
}
sum[i + 1] += (sum[i] + dp[i][-1 + n][0]) * (m - 1 - i);
}
dump(sum); dumpel(dp);
mint res = 0;
repi(j, 0, n - 1) res += dp[n][j - n + n][1];
res += dp[n][n - 1][1] * (m - n);
return res;
}
mint TLE5(int n, ll m) {
vvvm dp(n + 1, vvm(n, vm(2)));
dp[1][n - 1][0] = 1;
dp[1][n - 1][1] = 1;
mint sum = m;
repi(i, 1, n - 1) {
repi(j, 0, i) { // repi(j, 0, m - 1)
dp[i + 1][j - (i + 1) + n][0] += sum;
dp[i + 1][j - (i + 1) + n][1] += sum;
if (j > 0) {
dp[i + 1][j - (i + 1) + n][0] += dp[i][j - 1 - i + n][0] - dp[i][j - 1 - i + n][1];
dp[i + 1][j - (i + 1) + n][1] += dp[i][j - 1 - i + n][0];
}
}
sum = 0;
repi(j, 0, i) {
sum += dp[i + 1][j - (i + 1) + n][1];
}
sum += dp[i + 1][i - (i + 1) + n][1] * (m - 1 - i);
}
dump(sum); dumpel(dp);
mint res = 0;
repi(j, 0, n - 1) res += dp[n][j - n + n][1];
res += dp[n][n - 1][1] * (m - n);
return res;
}
//【正方行列(固定サイズ)】
/*
* Fixed_matrix<T, n>() : O(n^2)
* T の要素を成分にもつ n×n 零行列で初期化する.
*
* Fixed_matrix<T, n>(bool identity = true) : O(n^2)
* T の要素を成分にもつ n×n 単位行列で初期化する.
*
* Fixed_matrix<T, n>(vvT a) : O(n^2)
* 二次元配列 a[0..n)[0..n) の要素で初期化する.
*
* A + B : O(n^2)
* n×n 行列 A, B の和を返す.+= も使用可.
*
* A - B : O(n^2)
* n×n 行列 A, B の差を返す.-= も使用可.
*
* c * A / A * c : O(n^2)
* n×n 行列 A とスカラー c のスカラー積を返す.*= も使用可.
*
* A * x : O(n^2)
* n×n 行列 A と n 次元列ベクトル array<T, n> x の積を返す.
*
* x * A : O(n^2)
* n 次元行ベクトル array<T, n> x と n×n 行列 A の積を返す.
*
* A * B : O(n^3)
* n×n 行列 A と n×n 行列 B の積を返す.
*
* Mat pow(ll d) : O(n^3 log d)
* 自身を d 乗した行列を返す.
*/
template <class T, int n>
struct Fixed_matrix {
array<array<T, n>, n> v; // 行列の成分
// n×n 零行列で初期化する.identity = true なら n×n 単位行列で初期化する.
Fixed_matrix(bool identity = false) {
rep(i, n) v[i].fill(T(0));
if (identity) rep(i, n) v[i][i] = T(1);
}
// 二次元配列 a[0..n)[0..n) の要素で初期化する.
Fixed_matrix(const vector<vector<T>>& a) {
// verify : https://yukicoder.me/problems/no/1000
Assert(sz(a) == n && sz(a[0]) == n);
rep(i, n) rep(j, n) v[i][j] = a[i][j];
}
// 代入
Fixed_matrix(const Fixed_matrix&) = default;
Fixed_matrix& operator=(const Fixed_matrix&) = default;
// アクセス
inline array<T, n> const& operator[](int i) const { return v[i]; }
inline array<T, n>& operator[](int i) { return v[i]; }
// 入力
friend istream& operator>>(istream& is, Fixed_matrix& a) {
rep(i, n) rep(j, n) is >> a[i][j];
return is;
}
// 比較
bool operator==(const Fixed_matrix& b) const { return v == b.v; }
bool operator!=(const Fixed_matrix& b) const { return !(*this == b); }
// 加算,減算,スカラー倍
Fixed_matrix& operator+=(const Fixed_matrix& b) {
rep(i, n) rep(j, n) v[i][j] += b[i][j];
return *this;
}
Fixed_matrix& operator-=(const Fixed_matrix& b) {
rep(i, n) rep(j, n) v[i][j] -= b[i][j];
return *this;
}
Fixed_matrix& operator*=(const T& c) {
rep(i, n) rep(j, n) v[i][j] *= c;
return *this;
}
Fixed_matrix operator+(const Fixed_matrix& b) const { return Fixed_matrix(*this) += b; }
Fixed_matrix operator-(const Fixed_matrix& b) const { return Fixed_matrix(*this) -= b; }
Fixed_matrix operator*(const T& c) const { return Fixed_matrix(*this) *= c; }
friend Fixed_matrix operator*(const T& c, const Fixed_matrix& a) { return a * c; }
Fixed_matrix operator-() const { return Fixed_matrix(*this) *= T(-1); }
// 行列ベクトル積 : O(n^2)
array<T, n> operator*(const array<T, n>& x) const {
array<T, n> y{ 0 };
rep(i, n) rep(j, n) y[i] += v[i][j] * x[j];
return y;
}
// ベクトル行列積 : O(n^2)
friend array<T, n> operator*(const array<T, n>& x, const Fixed_matrix& a) {
array<T, n> y{ 0 };
rep(i, n) rep(j, n) y[j] += x[i] * a[i][j];
return y;
}
// 積:O(n^3)
Fixed_matrix operator*(const Fixed_matrix& b) const {
// verify : https://yukicoder.me/problems/no/1000
Fixed_matrix res;
rep(i, n) rep(j, n) rep(k, n) res[i][j] += v[i][k] * b[k][j];
return res;
}
Fixed_matrix& operator*=(const Fixed_matrix& b) { *this = *this * b; return *this; }
// 累乗:O(n^3 log d)
Fixed_matrix pow(ll d) const {
Fixed_matrix res(true), pow2(*this);
while (d > 0) {
if (d & 1) res *= pow2;
pow2 *= pow2;
d /= 2;
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const Fixed_matrix& a) {
rep(i, n) {
os << "[";
rep(j, n) os << a[i][j] << " ]"[j == n - 1];
if (i < n - 1) os << "\n";
}
return os;
}
#endif
};
//【行列乗算 作用付き ベクトル総和 可換モノイド】
constexpr int N134 = 3;
using T134 = mint;
using S134 = array<T134, N134>;
S134 op134(S134 x, S134 y) {
rep(i, N134) x[i] += y[i];
return x;
}
S134 e134() {
S134 x{ 0 };
return x;
}
using F134 = Fixed_matrix<T134, N134>;
S134 act134(F134 f, S134 x) { return f * x; }
F134 comp134(F134 f, F134 g) { return f * g; }
F134 id134() { return 1; }
#define Mul_Sum_mmonoid S134, op134, e134, F134, act134, comp134, id134
mint TLE6(int n, ll m) {
vector<S134> ini(n, { 0, 0, 1 });
ini[n - 1][0] = ini[n - 1][1] = 1;
lazy_segtree<Mul_Sum_mmonoid> seg(ini);
mint sum = m;
// dump(seg); dump(sum);
repi(i, 1, n - 1) {
seg.apply(0 - (i + 1) + n, 1 - (i + 1) + n, F134({ {0, 0, sum}, {0, 0, sum}, {0, 0, 1} }));
seg.apply(1 - (i + 1) + n, i - (i + 1) + n + 1, F134({ {1, -1, sum}, {1, 0, sum}, {0, 0, 1} }));
sum = seg.prod(0 - (i + 1) + n, (int)min<ll>(m, i + 1) - (i + 1) + n)[1];
if (m - 1 - i > 0) sum += seg.get(i - (i + 1) + n)[1] * (m - 1 - i);
// dump(seg); dump(sum);
}
return sum;
}
// ベタ書き
using T117 = mint;
using S117 = tuple<T117, T117, T117>;
using F117 = tuple<T117, T117, T117, T117, T117, T117>;
S117 op117(S117 x, S117 y) {
auto [vx, sx, cx] = x;
auto [vy, sy, cy] = y;
return { vx + vy, sx + sy, cx + cy };
}
S117 e117() { return { 0, 0, 0 }; }
S117 act117(F117 f, S117 xx) {
auto [x, y, z] = xx;
auto [a, b, s, c, d, t] = f;
return { a * x + b * y + s * z,c * x + d * y + t * z,z };
}
F117 comp117(F117 f, F117 g) {
auto [a, b, s, c, d, t] = f;
auto [a2, b2, s2, c2, d2, t2] = g;
return { a * a2 + b * c2, a * b2 + b * d2, s + a * s2 + b * t2,
a2 * c + c2 * d, b2 * c + d * d2, c * s2 + t + d * t2 };
}
F117 id117() { return { 1, 0, 0, 0, 1, 0 }; }
#define MMonoid S117, op117, e117, F117, act117, comp117, id117
mint solve(int n, ll m) {
vector<S117> ini(n, { 0, 0, 1 });
ini[n - 1] = { 1, 1, 1 };
lazy_segtree<MMonoid> seg(ini);
mint sum = m;
// dump(seg); dump(sum);
repi(i, 1, n - 1) {
seg.apply(0 - (i + 1) + n, { 0, 0, sum, 0, 0, sum });
seg.apply(1 - (i + 1) + n, i - (i + 1) + n + 1, { 1, -1, sum, 1, 0, sum });
sum = get<1>(seg.prod(0 - (i + 1) + n, (int)min<ll>(m, i + 1) - (i + 1) + n));
if (m - 1 - i > 0) sum += get<1>(seg.get(i - (i + 1) + n)) * (m - 1 - i);
// dump(seg); dump(sum);
}
return sum;
}
void bug_find() {
#ifdef _MSC_VER
// 合わない入力例を見つける.
mute_dump = true;
repi(n, 1, 5) repi(m, 1, 5) {
auto res_naive = naive(n, m);
auto res_solve = solve(n, m);
if (res_naive != res_solve) {
cout << "----------error!----------" << endl;
cout << "input:" << endl;
cout << n << " " << m << endl;
cout << "results:" << endl;
cout << res_naive << endl;
cout << res_solve << endl;
cout << "--------------------------" << endl;
}
}
mute_dump = false;
exit(0);
#endif
}
void zikken() {
int n = 10, m = 10;
vvm a(n, vm(m));
repi(i, 1, n) repi(j, 1, m) {
a[i - 1][j - 1] = TLE2(i, j);
}
dump_mat(a);
exit(0);
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
bug_find();
// zikken();
int n; ll m;
cin >> n >> m;
// dump(TLE(n, m)); dump("----");
// dump(TLE2(n, m)); dump("----");
// dump(TLE3(n, m)); dump("----");
// dump(TLE4(n, m)); dump("----");
// dump(TLE5(n, m)); dump("----");
cout << solve(n, m) << endl;
}