結果

問題 No.2443 特殊線形群の標準表現
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-25 22:18:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 604 ms / 3,000 ms
コード長 3,809 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 211,704 KB
実行使用メモリ 18,112 KB
最終ジャッジ日時 2023-08-25 22:18:58
合計ジャッジ時間 8,076 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 7 ms
4,384 KB
testcase_14 AC 58 ms
4,580 KB
testcase_15 AC 604 ms
17,900 KB
testcase_16 AC 584 ms
17,900 KB
testcase_17 AC 593 ms
17,804 KB
testcase_18 AC 576 ms
17,908 KB
testcase_19 AC 592 ms
17,840 KB
testcase_20 AC 532 ms
18,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename T>
using matrix = vector<vector<T>>;
ll modc;
class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {return pow(modc-2);}
    mint& operator/=(const mint& a){ return (*this) *= a.inv();}
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{ return x == a.x;}
    bool operator != (const mint& a) const{ return x != a.x;}
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint &m) {
        ll t;
        ip >> t;
        m = mint(t);
        return ip;
    }
    ll val(){ return x;}
};


template<typename T>
void show(matrix<T> &a){
    for (int i=0; i<a.size(); i++){
        for (int j=0; j<a[i].size(); j++) cout << a[i][j] << " ";
        cout << endl;
    }
}

template<typename T>
matrix<T> dot(matrix<T> &a, matrix<T> &b){
    int N = a.size();
    matrix<T> c(N, vector<T>(N));
    for (int i=0; i<N; i++){
        for (int k=0; k<N; k++){
            for (int j=0; j<N; j++) c[i][j] += (a[i][k] * b[k][j]);
        }
    }
    return c;
}

template<typename T>
T det(matrix<T> a){
    int N = a.size();
    T res = 1;
    for (int i=0; i<N; i++){
        if (a[i][i] == 0){
            for (int j=i+1; j<N; j++){
                if (a[i][j] != 0){
                    swap(a[i], a[j]);
                    res *= -1;
                    break;
                }
            }
            if (a[i][i] == 0) return 0;
        }
        res *= a[i][i];
        T iv = (T) 1 / a[i][i];
        for (int k=i; k<N; k++) a[i][k] *= iv;
        for (int j=i+1; j<N; j++){
            T mag = a[j][i];
            for (int k=i; k<N; k++){
                a[j][k] -= mag * a[i][k];
            }
        }
    }

    return res;
}

template<typename T>
matrix<T> pow(matrix<T> a, ll N){
    int M = a.size();
    matrix<T> b(M, vector<T>(M));
    for (int i=0; i<M; i++) b[i][i] = 1;
    while(N){
        if (N % 2 == 1) b = dot(b, a);
        N >>= 1;
        a = dot(a, a);
    }
    return b;
}

template<typename T>
matrix<T> inverse(matrix<T> &a){
    T p, q, r, s, t;
    p = a[0][0];
    q = a[0][1];
    r = a[1][0];
    s = a[1][1];
    matrix<T> res = {{s, -q}, {-r, p}};
    return res;
}

int main(){

    ll N, B, Q, l, r;
    mint x, y;
    cin >> N >> B >> Q;
    modc = B;

    vector<matrix<mint>> s(N+1);
    s[0] = {{1, 0}, {0, 1}};

    for (int i=1; i<=N; i++){
        matrix<mint> a(2, vector<mint>(2));
        for (int j=0; j<2; j++) for (int k=0; k<2; k++) cin >> a[j][k];
        s[i] = dot(a ,s[i-1]);
    }

    while(Q){
        Q--;
        cin >> l >> r >> x >> y;
        matrix<mint> a=s[r], b=inverse(s[l]), c;
        c = dot(a, b);
        mint p=x*c[0][0]+y*c[0][1], q=x*c[1][0]+y*c[1][1];
        cout << p << " " << q << endl;
    }

    return 0;
}
0