結果
| 問題 |
No.2503 Typical Path Counting Problem on a Grid
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-07-24 17:01:53 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,095 bytes |
| コンパイル時間 | 1,500 ms |
| コンパイル使用メモリ | 105,280 KB |
| 実行使用メモリ | 817,808 KB |
| 最終ジャッジ日時 | 2024-10-03 08:44:53 |
| 合計ジャッジ時間 | 5,812 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | MLE * 1 -- * 9 |
ソースコード
#include <cassert>
#include <cstdint>
#include <iostream>
#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
int main() {
using mint = atcoder::modint998244353;
constexpr int kMaxT = 20000, kMaxN = 10000000;
constexpr std::int64_t kMaxM = 1000000000000000000;
std::vector dp1(kMaxN + 1, emthrm::Matrix<mint>(2, 2));
dp1[0][0][0] = dp1[0][1][1] = 1;
for (int i = 0; i < kMaxN; ++i) {
dp1[i + 1][0][0] = (i + 1) * 2;
dp1[i + 1][0][1] = 1;
dp1[i + 1][1][0] = i + 1;
dp1[i + 1] *= dp1[i];
}
std::vector dp2(kMaxN + 1, emthrm::Matrix<mint>(2, 2));
dp2[0][0][0] = dp2[0][1][1] = 1;
for (int i = 0; i < kMaxN; ++i) {
dp2[i + 1][0][0] = (i + 1) * 2;
dp2[i + 1][0][1] = 1;
dp2[i + 1][1][0] = i;
dp2[i + 1] = dp2[i] * dp2[i + 1];
}
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);
if (n > m) {
const int tmp = m;
m = n;
n = tmp;
}
emthrm::Matrix<mint> matrix(2, 2);
matrix[0][0] = n * 2 + 1;
matrix[0][1] = 1;
matrix[1][0] = n;
std::cout << (dp2[n] * matrix.pow(m - n) * dp1[n])[0][0].val() << '\n';
}
return 0;
}
emthrm