結果

問題 No.2503 Typical Path Counting Problem on a Grid
ユーザー milanis48663220milanis48663220
提出日時 2023-10-14 01:40:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 726 ms / 2,000 ms
コード長 4,377 bytes
コンパイル時間 2,488 ms
コンパイル使用メモリ 122,700 KB
実行使用メモリ 316,116 KB
最終ジャッジ日時 2023-10-14 01:40:16
合計ジャッジ時間 11,770 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 632 ms
315,892 KB
testcase_01 AC 675 ms
315,860 KB
testcase_02 AC 659 ms
315,860 KB
testcase_03 AC 666 ms
315,844 KB
testcase_04 AC 717 ms
315,920 KB
testcase_05 AC 668 ms
315,844 KB
testcase_06 AC 722 ms
316,032 KB
testcase_07 AC 700 ms
315,896 KB
testcase_08 AC 703 ms
316,116 KB
testcase_09 AC 726 ms
316,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}

template<typename T, int N, int M>
class Matrix {
    public:
    array<array<T, M>, N> dat;
    Matrix(T val=0) {
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++){
                dat[i][j] = val;
            }
        }
    }

    Matrix(array<array<T, M>, N> dat): dat(dat){  }
    array<T, M>& operator[](int x) {
        return dat[x];
	}
};

template<typename T, int N, int M, int K>
Matrix<T, N, K> operator*(Matrix<T, N, M> a, Matrix<T, M, K> b){
    Matrix<T, N, K> c(T(0));
    for(int i = 0; i < N; i++){
        for(int j = 0; j < K; j++){
            for(int k = 0; k < M; k++){
                c.dat[i][j] += a.dat[i][k]*b.dat[k][j];
            }
        }
    }
    return c;
}

template<typename T, int N>
Matrix<T, N, N> operator^(Matrix<T, N, N> m, long long r){
    Matrix<T, N, N> ans(T(0));
    for(int i = 0; i < N; i++) ans[i][i] = T(1);
    while (r > 0) {
        if (r&1) ans = (ans*m);
        m = (m*m);
        r >>= 1;
    }
    return ans;
}

template <typename T, int N, int M>
void print_mat(Matrix<T, N, M> a){
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            cout << a.dat[i][j] << ' ';
        }
        cout << endl;
    }
}

template <typename T, int N, int M>
ostream& operator<<(ostream& os, const Matrix<T, N, M>& m){
    print_mat<T, N, M>(m);
    return os;
}

mint naive(ll n, ll m){
    if(n > m) swap(n, m);
    if(n == 0) return mint(1);
    vector<mint> dp(n+m+1);
    dp[0] = 1;
    dp[1] = 2;
    for(ll x = 2; x <= n+m; x++){
        if(x <= n){
            dp[x] += dp[x-1]*2*x;
            dp[x] += dp[x-2]*(x-1);
        }else if(x <= m){
            dp[x] += dp[x-1]*(2*n+1);
            dp[x] += dp[x-2]*n;
        }else{
            int c = n+m-x+1;
            dp[x] += dp[x-1]*2*c;
            dp[x] += dp[x-2]*c;
        }
    }
    // print_vector(dp);
    return dp[n+m];
}

const int N = 10000000;

using M = Matrix<mint, 2, 2>;
using V = Matrix<mint, 2, 1>;

M f0[N+1];
M f1[N+1];

void init(){
    f0[1] = M({{
            {mint(1), mint(0)},
            {mint(0), mint(1)},
    }});
    for(int x = 2; x <= N; x++){
        f0[x] = M({{
            {mint(2*x), mint(x-1)},
            {mint(1), mint(0)},
        }})*f0[x-1];
    }

    f1[0] = M({{
            {mint(1), mint(0)},
            {mint(0), mint(1)},
    }});
    for(int x = 1; x <= N; x++){
        f1[x] = f1[x-1]*M({{
            {mint(2*x), mint(x)},
            {mint(1), mint(0)},
        }});
    }
}

mint solve(ll n, ll m){
    if(n > m) swap(n, m);
    if(n == 0) return mint(1);
    mint dp1 = 2;
    mint dp0 = 1;
    V v({{
        {dp1},
        {dp0}
    }});
    v = f0[n]*v;
    M A({{
        {mint(2*n+1), mint(n)},
        {mint(1), mint(0)},
    }});
    v = (A^(m-n))*v;
    v = f1[n]*v;
    return v[0][0];
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    init();
    int t; cin >> t;
    while(t--) {
        ll n, m; cin >> n >> m;
        cout << solve(n, m) << endl;
    }
}
0