結果

問題 No.3006 ベイカーの問題
ユーザー 👑 Nachia
提出日時 2025-01-17 21:42:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,944 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 83,080 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2025-01-17 21:42:41
合計ジャッジ時間 2,044 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
#include <atcoder/modint>
using Modint = atcoder::static_modint<998244353>;
#include <cassert>
#include <utility>

namespace nachia{

template<class Elem>
struct MatrixOnRing{
private:
    int h;
    int w;
    std::vector<Elem> elems;

public:
    
    MatrixOnRing(int new_h=0, int new_w=0){ h = new_h; w = new_w; elems.resize(h * w); }
    MatrixOnRing(MatrixOnRing const&) = default;
    int numRow() const { return h; }
    int numColumn() const { return w; }
    int height() const { return numRow(); }
    int width() const { return numColumn(); }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y*w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y*w); }
    static MatrixOnRing Identity(int idx, Elem One){ auto res = MatrixOnRing(idx, idx); for(int i=0; i<idx; i++) res[i][i] = One; return res; }
    void swapColumns(int x1, int x2){
        assert(0 <= x1 && x1 < numColumn());
        assert(0 <= x2 && x2 < numColumn());
        if(x1 == x2) return;
        for(int y=0; y<numRow(); y++) std::swap((*this)[y][x1], (*this)[y][x2]);
    }
    void swapRows(int y1, int y2){
        assert(0 <= y1 && y1 < numRow());
        assert(0 <= y2 && y2 < numRow());
        if(y1 == y2) return;
        for(int x=0; x<numColumn(); x++) std::swap((*this)[y1][x], (*this)[y2][x]);
    }
    MatrixOnRing operator*(const MatrixOnRing& r) const {
        assert(width() == r.height());
        auto res = MatrixOnRing(h, r.w);
        for(int i=0; i<h; i++) for(int j=0; j<w; j++) for(int k=0; k<r.w; k++) res[i][k] = res[i][k] + (*this)[i][j] * r[j][k];
        return res;
    }
    MatrixOnRing pow(unsigned long long i){
        if(i == 1) return *this;
        auto a = *this;
        auto res = a; i--;
        while(i){
            if(i % 2 == 1) res = res * a;
            a = a * a;
            i /= 2;
        }
        return res;
    }
};

} // namespace nachia

void testcase(){
    Modint x,y; { i64 a,b; cin >> a >> b; x = a; y = b; }
    i64 N; cin >> N;
    nachia::MatrixOnRing<Modint> A(4,4);
    A[0][2] = 1;
    A[1][3] = 1;
    A[2][2] = 1;
    A[3][3] = 1;
    A[0][0] = x * 1;
    A[1][0] = y * -5;
    A[0][1] = y * 1;
    A[1][1] = x * 1;
    A = A.pow(N);
    Modint rx = x * A[0][2] + y * A[1][2];
    Modint ry = x * A[0][3] + y * A[1][3];
    cout<< rx.val() << " " << ry.val() << "\n";
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}
0