結果
問題 | No.950 行列累乗 |
ユーザー | やむなく |
提出日時 | 2019-12-13 02:49:14 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,177 bytes |
コンパイル時間 | 2,503 ms |
コンパイル使用メモリ | 202,820 KB |
実行使用メモリ | 67,840 KB |
最終ジャッジ日時 | 2024-06-26 05:04:13 |
合計ジャッジ時間 | 39,345 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 208 ms
5,376 KB |
testcase_02 | AC | 222 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 22 ms
5,504 KB |
testcase_06 | AC | 338 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 310 ms
5,376 KB |
testcase_09 | AC | 249 ms
5,376 KB |
testcase_10 | WA | - |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 203 ms
5,376 KB |
testcase_13 | AC | 332 ms
5,376 KB |
testcase_14 | WA | - |
testcase_15 | AC | 255 ms
5,376 KB |
testcase_16 | WA | - |
testcase_17 | AC | 217 ms
5,376 KB |
testcase_18 | AC | 207 ms
5,376 KB |
testcase_19 | AC | 205 ms
5,376 KB |
testcase_20 | AC | 210 ms
5,376 KB |
testcase_21 | AC | 1,663 ms
67,712 KB |
testcase_22 | WA | - |
testcase_23 | AC | 23 ms
5,632 KB |
testcase_24 | AC | 1,781 ms
67,712 KB |
testcase_25 | WA | - |
testcase_26 | AC | 26 ms
5,888 KB |
testcase_27 | AC | 230 ms
5,632 KB |
testcase_28 | WA | - |
testcase_29 | AC | 1,806 ms
67,840 KB |
testcase_30 | AC | 1,828 ms
67,712 KB |
testcase_31 | AC | 1,425 ms
67,712 KB |
testcase_32 | WA | - |
testcase_33 | AC | 1,354 ms
67,712 KB |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | AC | 216 ms
5,376 KB |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | AC | 216 ms
5,376 KB |
testcase_40 | WA | - |
testcase_41 | AC | 34 ms
6,272 KB |
testcase_42 | AC | 1,904 ms
67,712 KB |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | AC | 1,930 ms
67,712 KB |
testcase_46 | WA | - |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | WA | - |
testcase_52 | WA | - |
testcase_53 | WA | - |
testcase_54 | AC | 218 ms
5,376 KB |
testcase_55 | AC | 2 ms
5,376 KB |
testcase_56 | WA | - |
testcase_57 | AC | 2 ms
5,376 KB |
testcase_58 | WA | - |
testcase_59 | AC | 2 ms
5,376 KB |
testcase_60 | AC | 2 ms
5,376 KB |
ソースコード
// // 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] % m - v[0][1] * v[1][0] % m) % m + m) % m; } 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) + 10; ll ar = inv(modpow(a, r, p), p); map<ll, ll> mp; ll tmp = b; rep(i, r){ auto itr = mp.find(tmp); if(itr == mp.end()) 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) % (p - 1)); tmp = tmp * a % p; } return ans; } mtl mtinv(mtl a, ll p){ ll d = inv(det(a, p), p); return { {a[1][1] * d % p, (-a[0][1] * d % p + p) % p}, {(-a[1][0] * d % p + p) % p, a[0][0] * d % p}, }; } ll mtlog(mtl a, mtl b, ll p){ // a^(i*r+j)=b // a^j = b/a^(i*r) ll r = sqrt(1e11); mtl ar = mtinv(mtpow(a, r, p), p); map<mtl, ll> mp; mtl tmp = b; rep(i, r){ auto itr = mp.find(tmp); if(itr == mp.end()) mp[tmp] = i; tmp = prod(tmp, ar, p); } ll ans = LINF; tmp = { {1, 0}, {0, 1} }; rep(i, r){ auto itr = mp.find(tmp); if(itr != mp.end()) ans = min(ans, itr->second * r + i); tmp = prod(tmp, a, p); } return ans; } void answer(ll x, ll y, ll z){ ll x1 = x % IINF, x2 = x / IINF; ll y1 = y % IINF, y2 = y / IINF; ll ans1 = x1 * y1 + z; ll c = ans1 / IINF; ans1 %= IINF; ll ans2 = x1 * y2 + x2 * y1 + c; c = ans2 / IINF; ll ans3 = x2 * y2 + c; if(ans3>0) cout << ans3; if(ans2>0) cout << ans2; cout << ans1 << endl; } 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){ cout << 1 << endl; return 0; } ll x = det(a, p), y = det(b, p); if(x == 0){ if(y > 0){ cout << -1 << endl; return 0; } ll ans = mtlog(a, b, p); if(ans == LINF) cout << -1 << endl; else cout << ans << endl; return 0; } if(y == 0){ cout << -1 << endl; return 0; } ll l = nlog(x, y, p); mtl tmp = mtpow(a, l, p); if(tmp == b){ cout << l << endl; return 0; } a = mtpow(a, p - 1, p); b = prod(b, mtinv(tmp, p), p); ll ans = mtlog(a, b, p); if(ans == LINF) cout << -1 << endl; else answer(ans, p-1, l); return 0; }