結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー kwm_tkwm_t
提出日時 2023-10-18 23:13:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 2,065 bytes
コンパイル時間 4,579 ms
コンパイル使用メモリ 267,884 KB
実行使用メモリ 42,408 KB
最終ジャッジ日時 2023-10-18 23:13:19
合計ジャッジ時間 8,551 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
42,408 KB
testcase_01 AC 129 ms
42,408 KB
testcase_02 AC 109 ms
42,408 KB
testcase_03 AC 217 ms
42,408 KB
testcase_04 AC 326 ms
42,408 KB
testcase_05 AC 188 ms
42,408 KB
testcase_06 AC 380 ms
42,408 KB
testcase_07 AC 382 ms
42,408 KB
testcase_08 AC 251 ms
42,408 KB
testcase_09 AC 353 ms
42,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
const int mod = 998244353;
//using mint = modint1000000007;
//const int mod = 1000000007;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
template<typename T>
vector<vector<T>> mul(vector<vector<T>>&A, vector<vector<T>>&B) {
	vector<vector<T>> C(A.size(), vector<T>(B[0].size()));
	for (int i = 0; i < (int)A.size(); ++i) {
		for (int j = 0; j < (int)B.size(); ++j) {
			for (int k = 0; k < (int)C.size(); ++k) C[i][j] += A[i][k] * B[k][j];
		}
	}
	return C;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t; cin >> t;
	vector<mint>f(10000007);
	f[0] = 1;
	f[1] = 2;
	rep2(i, 2, 10000007) f[i] = 2 * i * f[i - 1] + (i - 1) * f[i - 2];
	while (t--) {
		long long n, m; cin >> n >> m;
		if (n > m)swap(n, m);
		if (0 == n) {
			cout << 1 << endl;
			continue;
		}
		// ans = f[m] * f[n] + f[m-1] * f[n-1] * n;
		vector<vector<mint>> mans(2, vector<mint>(2));
		rep(i, 2)mans[i][i] = 1;
		vector<vector<mint>> mpow(2, vector<mint>(2));
		mpow[0][0] = 0, mpow[0][1] = 1;
		mpow[1][0] = n, mpow[1][1] = 2 * n + 1;
		//mpow初期化
		long long x = m - n;
		while (x > 0) {
			if (1 == x % 2) mans = mul(mpow, mans);
			mpow = mul(mpow, mpow);
			x /= 2;
		}
		mint g0 = f[n - 1] * mans[0][0] + f[n] * mans[0][1];
		mint g1 = f[n - 1] * mans[1][0] + f[n] * mans[1][1];
		mint ans = 0;
		ans += g1 * f[n];
		ans += g0 * f[n - 1] * n;
		cout << ans.val() << endl;
	}
	return 0;
}
0