結果

問題 No.950 行列累乗
ユーザー LeonardoneLeonardone
提出日時 2019-12-13 04:06:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,853 bytes
コンパイル時間 1,222 ms
コンパイル使用メモリ 100,096 KB
実行使用メモリ 26,432 KB
最終ジャッジ日時 2023-09-08 12:50:08
合計ジャッジ時間 12,054 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 153 ms
17,648 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 192 ms
22,752 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 118 ms
13,764 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 213 ms
25,660 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 211 ms
25,580 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 216 ms
26,008 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 207 ms
25,700 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 217 ms
25,688 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 207 ms
25,580 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 216 ms
25,504 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 218 ms
25,656 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 215 ms
25,604 KB
testcase_46 WA -
testcase_47 AC 1 ms
4,380 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 WA -
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 214 ms
25,656 KB
testcase_60 AC 215 ms
25,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;


string tos(vector<long long> &a) {
    return to_string(a[0])+","+to_string(a[1])+","+to_string(a[2])+","+to_string(a[3]);
}

void mul(vector<long long> &a, vector<long long> &c, long long p) {
    vector<long long> d(4);
    d[0] = (a[0] * c[0] % p + a[1] * c[2] % p) % p;
    d[1] = (a[0] * c[1] % p + a[1] * c[3] % p) % p;
    d[2] = (c[0] * a[2] % p + c[2] * a[3] % p) % p;
    d[3] = (c[1] * a[2] % p + c[3] * a[3] % p) % p;
    a = d;
}

void modpow(vector<long long> &a, long long k, long long p) {
    if (k > 1) {
        if (k&1) {
            vector<long long> c(a);
            modpow(a, k-1, p);
            mul(a, c, p);
        } else {
            mul(a, a, p);
            modpow(a, k/2, p);
        }
    }
}

int main() {
    long long p;
    vector<long long> a(4), b(4);
    cin >> p;
    for (int i = 0; i < 4; i++) cin >> a[i];
    for (int i = 0; i < 4; i++) cin >> b[i];
    if (a == b) {
        cout << 1 << endl; return 0;
    }
    vector<long long> c(a);
    vector<vector<long long>> s1;
    s1.push_back(a);
    vector<long long> e(a), f(b);
    modpow(e, p-2, p);
    unordered_map<string, int> s2;
    for (int i=1; i <= 1e5; i++) {
        mul(a, c, p);
        s1.push_back(a);
        mul(f, e, p);
        s2[tos(f)] = i;
        if (a == b) {
            cout << (i+1) << endl;
            return 0;
        }
        if (a == c) {
            cout << (-1) << endl;
            return 0;
        }
        
        // cout << a[0] << "," << a[1] << "," << a[2] << "," << a[3] << endl;
    }
    for (int i = 0; i < s1.size(); i++) {
        auto y = s2.find(tos(s1[i]));
        if (y != s2.end()) {
            cout << ((i+1)*y->second) << endl;
            return 0;
        }
    }
    cout << (-1) << endl;
    return 0;
}
0