結果

問題 No.2529 Treasure Hunter
ユーザー ococonomy1ococonomy1
提出日時 2023-11-03 22:26:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 3,232 bytes
コンパイル時間 1,337 ms
コンパイル使用メモリ 117,292 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-11-03 22:26:39
合計ジャッジ時間 2,192 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 12 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define TEST cerr << "TEST" << endl
#define AMARI 998244353
// #define AMARI 1000000007
#define TIME_LIMIT 1980000
#define el '\n'
#define El '\n'


// n*nの正方行列。 ococo_gyouretu<型の名前> og(n);で宣言
// 足し算・掛け算・べき乗(O(logN)のやつ)しかない。
template <typename T> class ococo_gyouretu {
  public:
    int n;
    // ここに扱うデータが入ってる
    vector<vector<T>> a;
    ococo_gyouretu(int N) {
        n = N;
        a.resize(n);
        for(int i = 0; i < n; i++) a[i].resize(n);
    }
    ococo_gyouretu operator+(ococo_gyouretu const &r) const {
        ococo_gyouretu<T> ans(n);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                ans[i][j] += a[i][j] + r.a[i][j];
                if(ans[i][j] >= AMARI) ans[i][j] -= AMARI;
            }
        }
        return ans;
    }
    ococo_gyouretu operator*(ococo_gyouretu const &r) const {
        ococo_gyouretu<T> ans(n);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                for(int k = 0; k < n; k++) {
                    ans.a[i][j] += a[i][k] * r.a[k][j];
                    ans.a[i][j] %= AMARI;
                }
            }
        }
        return ans;
    }
    ococo_gyouretu bekizyou(ll b) {
        ococo_gyouretu<T> temp(n), ans(n);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                temp.a[i][j] = a[i][j];
                ans.a[i][j] = (i == j ? 1 : 0);
            }
        }
        while(b) {
            if(b % 2) {
                ans = ans * temp;
            }
            temp = temp * temp;
            b /= 2;
        }
        return ans;
    }
};

#define MULTI_TEST_CASE true
void solve(void) {
    ll w,h;
    cin >> w >> h;
    ococo_gyouretu<ll> g(3);
    //g[i][j] = ある行にi個あった時、次の行にj個置くのが何通りか
    //gは遷移行列
    g.a[0][0] = g.a[1][0] = g.a[2][0] = 1;
    g.a[0][1] = w; g.a[0][2] = max((w * (w - 1)) / 2 - w,0LL);
    g.a[1][1] = w - 1; g.a[1][2] = max(((w - 1) * (w - 2)) / 2 - (w - 2),0LL);
    g.a[2][1] = w - 2; g.a[2][2] = max(((w - 2) * (w - 3)) / 2 - (w - 4),0LL);
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            g.a[i][j] %= AMARI;
        }
    }
    ococo_gyouretu<ll> ans = g.bekizyou(h);
    ll ansv = 0;
    for(int i = 0; i < 3; i++){
        ansv += ans.a[0][i];
        if(ansv < 0)ansv += AMARI;
        ansv %= AMARI;
    }
    cout << ansv << el;
    return;
}

void calc(void) {
    return;
}

signed main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE) cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
0