結果

問題 No.950 行列累乗
ユーザー やむなくやむなく
提出日時 2019-12-13 01:31:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,100 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 184,264 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-08 09:48:51
合計ジャッジ時間 6,289 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 RE -
testcase_04 RE -
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,380 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 1 ms
4,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 1 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 1 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,380 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 2 ms
4,380 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 1 ms
4,384 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 1 ms
4,376 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 WA -
testcase_59 RE -
testcase_60 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by yamunaku on 2019/12/13.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

ll p;
mtl a, b;

ll det(mtl &v, ll m){
    return ((v[0][0] * v[1][1] - v[0][1] * v[1][0]) % m + m) % m;
}

ll gcd(ll x, ll y){
    while(x % y){
        ll tmp = x % y;
        x = y;
        y = tmp;
    }
    return y;
}

mtl prod(mtl a, mtl b, ll m){
    return {
            {(a[0][0] * b[0][0] + a[0][1] * b[1][0]) % m, (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % m},
            {(a[1][0] * b[0][0] + a[1][1] * b[1][0]) % m, (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % m}
    };
}

mtl mtpow(mtl v, ll x, ll m){
    mtl ans = {
            {1, 0},
            {0, 1}
    };
    while(x){
        if(x & 1) ans = prod(ans, v, m);
        x >>= 1;
        v = prod(v, v, m);
    }
    return ans;
}

ll modpow(ll x, ll a, ll m){
    ll ans = 1;
    while(a){
        if(a & 1) ans = ans * x % m;
        a >>= 1;
        x = x * x % m;
    }
    return ans;
}

ll inv(ll x, ll m){
    return modpow(x, m - 2, m);
}


ll nlog(ll a, ll b, ll p){
    // a^(i*r+j)=b
    // a^j = b/a^(i*r)
    ll r = sqrt(p) + 1;
    ll ar = inv(modpow(a, r, p), p);
    map<ll, ll> mp;
    ll tmp = b;
    rep(i, r){
        mp[tmp] = i;
        tmp = tmp * ar % p;
    }
    ll ans = LINF;
    tmp = 1;
    rep(i, r){
        auto itr = mp.find(tmp);
        if(itr != mp.end()) ans = min(ans, itr->second * r + i);
        tmp = tmp * a % p;
    }
    return ans;
}


int main(){
    cin >> p;
    a = b = mtl(2, vl(2));
    rep(i, 2) rep(j, 2) cin >> a[i][j];
    rep(i, 2) rep(j, 2) cin >> b[i][j];
    if(a == b){
        assert(false);
        cout << 1 << endl;
        return 0;
    }

    ll x = det(a, p), y = det(b, p);
    if(x == 0){
        assert(false);
        if(y > 0){
            cout << -1 << endl;
            return 0;
        }
        mtl tmp = {
                {1, 0},
                {0, 1}
        };
        mtl zero = {
                {0, 0},
                {0, 0}
        };
        for(int i = 1; tmp != zero; i++){
            tmp = prod(tmp, a, p);
            if(tmp == b){
                cout << i << endl;
                return 0;
            }
        }
        cout << -1 << endl;
        return 0;
    }

    cout << -1 << endl;
    return 0;
}
0