結果
| 問題 |
No.2503 Typical Path Counting Problem on a Grid
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-07-24 15:47:13 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,124 bytes |
| コンパイル時間 | 1,165 ms |
| コンパイル使用メモリ | 101,776 KB |
| 実行使用メモリ | 13,636 KB |
| 最終ジャッジ日時 | 2024-10-11 12:40:53 |
| 合計ジャッジ時間 | 5,130 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 2 TLE * 1 -- * 7 |
ソースコード
#include <cassert>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>
#include <atcoder/modint>
namespace emthrm {
template <typename T>
struct Matrix {
explicit Matrix(const int m, const int n, const T def = 0)
: data(m, std::vector<T>(n, def)) {}
int nrow() const { return data.size(); }
int ncol() const { return data.front().size(); }
Matrix pow(long long exponent) const {
const int n = nrow();
Matrix<T> res(n, n, 0), tmp = *this;
for (int i = 0; i < n; ++i) {
res[i][i] = 1;
}
for (; exponent > 0; exponent >>= 1) {
if (exponent & 1) res *= tmp;
tmp *= tmp;
}
return res;
}
inline const std::vector<T>& operator[](const int i) const { return data[i]; }
inline std::vector<T>& operator[](const int i) { return data[i]; }
Matrix& operator=(const Matrix& x) = default;
Matrix& operator+=(const Matrix& x) {
const int m = nrow(), n = ncol();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
data[i][j] += x[i][j];
}
}
return *this;
}
Matrix& operator-=(const Matrix& x) {
const int m = nrow(), n = ncol();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
data[i][j] -= x[i][j];
}
}
return *this;
}
Matrix& operator*=(const Matrix& x) {
const int m = nrow(), l = ncol(), n = x.ncol();
std::vector<std::vector<T>> res(m, std::vector<T>(n, 0));
for (int i = 0; i < m; ++i) {
for (int k = 0; k < l; ++k) {
for (int j = 0; j < n; ++j) {
res[i][j] += data[i][k] * x[k][j];
}
}
}
data.swap(res);
return *this;
}
Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }
private:
std::vector<std::vector<T>> data;
};
} // namespace emthrm
using mint = atcoder::modint998244353;
// <TLE>
// O(min(n, m) + log|n - m|) 時間
mint Solve(const int n, const std::int64_t m) {
if (n > m) return Solve(m, n);
mint dp0 = 1, dp1 = 0;
for (int diag = 0; diag < n; ++diag) {
dp1 += dp0 * (diag + 1) * 2;
dp0 *= diag + 1;
std::swap(dp0, dp1);
}
emthrm::Matrix<mint> matrix(2, 2);
matrix[0][0] = n * 2 + 1;
matrix[0][1] = 1;
matrix[1][0] = n;
matrix = matrix.pow(m - n);
const mint next_dp0 = dp0 * matrix[0][0] + dp1 * matrix[0][1];
const mint next_dp1 = dp0 * matrix[1][0] + dp1 * matrix[1][1];
dp0 = next_dp0;
dp1 = next_dp1;
for (std::int64_t diag = m; diag < n + m; ++diag) {
dp1 += dp0 * (n + m - diag) * 2;
dp0 *= n + m - diag - 1;
std::swap(dp0, dp1);
}
return dp0;
}
int main() {
constexpr int kMaxT = 20000, kMaxN = 10000000;
constexpr std::int64_t kMaxM = 1000000000000000000;
int t;
std::cin >> t;
assert(1 <= t && t <= kMaxT);
while (t--) {
int n;
std::int64_t m;
std::cin >> n >> m;
assert(0 <= n && n <= kMaxN && 0 <= m && m <= kMaxM);
std::cout << Solve(n, m).val() << '\n';
}
return 0;
}
emthrm