結果
問題 | No.950 行列累乗 |
ユーザー | heno239 |
提出日時 | 2019-12-13 02:41:52 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,313 bytes |
コンパイル時間 | 1,818 ms |
コンパイル使用メモリ | 127,500 KB |
実行使用メモリ | 229,696 KB |
最終ジャッジ日時 | 2024-06-26 04:52:36 |
合計ジャッジ時間 | 8,388 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
ソースコード
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define Rep(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) Rep(i,0,n) #define stop char nyaa;cin>>nyaa; using ll = long long; const int inf = 1e9 + 7; const ll INF = 1LL << 60; //const ll mod = 1e9 + 7; using P = pair<int, int>; using LP = pair<ll, ll>; ll p; typedef vector<vector<ll>> mat; typedef vector<ll> vec; mat mtmul(mat &A, mat &B) { mat C(A.size(), vec(B[0].size())); rep(i, (int)A.size()) { rep(k, (int)B.size()) { rep(j, (int)B[0].size()) { C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % p) % p; } } } return C; } mat mtpow(mat A, ll n) { mat B(A.size(), vec(A.size())); rep(i, (int)A.size()) { B[i][i] = 1; } while (n > 0) { if (n&(ll)1)B = mtmul(B, A); A = mtmul(A, A); n >>= 1; } return B; } ll dat(mat A) { ll res = A[0][0] * A[1][1] - A[0][1] * A[1][0]; res = (res%p + p) % p; return res; } ll mod_pow(ll x, ll n) { ll res = 1; while (n > 0) { if (n % 2)res = res * x%p; x = x * x%p; n >>= 1; } return res; } //a^res=b (mod p) ll ident(ll a, ll b) { if (b == 1)return 0; if (p <= 100000) { int tmp = 1; ll cop = a; while (cop != b) { cop = cop * a%p; tmp++; } return tmp; } ll d = sqrt(p); map<ll, int> mp; ll cop = b; ll inva = mod_pow(a, p - 2); for (int y = 0; y < d; y++) { mp[cop] = y; cop = cop * inva%p; } ll spea = mod_pow(a, d); cop = 1; for (int x = 0; x <= d; x++) { if (mp.find(cop)!=mp.end()) { ll res = x * d + mp[cop]; res %= (p - 1); return res; } cop = cop * spea%p; } assert(false); return 0; } void solve() { mat A(2), B(2); cin >> p; rep(i, 2) { A[i].resize(2); rep(j, 2)cin >> A[i][j]; } rep(i, 2) { B[i].resize(2); rep(j, 2)cin >> B[i][j]; } ll da = dat(A), db = dat(B); map<mat, bool> mp; if (da == 0) { if (db > 0) { cout << -1 << endl; return; } mat cop = A; if (cop == B) { cout << 1 << endl; return; } mp[cop] = true; int tmp = 1; while (true) { tmp++; cop = mtmul(cop, A); if (mp[cop]) { cout << -1 << endl; return; } if (cop == B) { cout << tmp << endl; return; } mp[cop] = true; } } if (da == 1) { mat cop = A; if (db != 1) { cout << -1 << endl; return; } if (cop == B) { cout << 1 << endl; return; } mp[cop] = true; int tmp = 1; while (true) { tmp++; cop = mtmul(cop, A); if (mp[cop]) { cout << -1 << endl; return; } if (cop == B) { cout << tmp << endl; return; } mp[cop] = true; } } if (db == 0) { cout << -1 << endl; return; } ll k = ident(da, db); ll res = k; assert(0 < k&&k <= p - 1); mat cop = mtpow(A, k); mat Z = mtpow(A, p - 1); //cout << Z[0][0] << " " << Z[0][1] << " " << Z[1][0] << " " << Z[1][1] << endl; if (cop == B) { cout << k << endl; return; } mp[cop] = true; while (true) { res += p - 1; cop = mtmul(cop, Z); if (cop == B) { cout << res << endl; return; } if (mp[cop]) { cout << -1 << endl; return; } mp[cop] = true; } } signed main() { solve(); stop return 0; }