結果
| 問題 |
No.950 行列累乗
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2019-11-27 16:26:35 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,948 bytes |
| コンパイル時間 | 1,388 ms |
| コンパイル使用メモリ | 124,824 KB |
| 実行使用メモリ | 10,496 KB |
| 最終ジャッジ日時 | 2024-11-08 11:01:41 |
| 合計ジャッジ時間 | 6,576 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 2 |
| other | AC * 30 WA * 24 TLE * 1 -- * 2 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, int> P;
ll p;
using arr=array<ll, 2>;
using Mat=array<arr, 2>;
Mat matmul(Mat a, Mat b){
Mat c={};
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
for(int k=0; k<2; k++){
(c[i][j]+=a[i][k]*b[k][j])%=p;
}
}
}
return c;
}
Mat matpow(Mat a, ll k){
Mat ap=a, ans={};
ans[0][0]=ans[1][1]=1;
while(k){
if(k&1) ans=matmul(ap, ans);
ap=matmul(ap, ap);
k>>=1;
}
return ans;
}
Mat e;
ll discretelog(Mat a, Mat b, ll d){
ll sq=1;
while(sq*sq<d) sq++;
vector<pair<Mat, int>> vp(sq);
vp[0]=make_pair(e, 0);
for(int i=1; i<sq; i++){
vp[i]=make_pair(matmul(vp[i-1].first, a), i);
}
auto cp=[](pair<Mat, int> x, pair<Mat, int> y){
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
if(x.first[i][j]!=y.first[i][j]) return x.first[i][j]<y.first[i][j];
}
}
return false;
};
sort(vp.begin(), vp.end(), cp);
Mat ap=matpow(matpow(a, d-1), sq);
for(int i=0; i<sq; i++){
int t=lower_bound(vp.begin(), vp.end(), make_pair(b, 0), cp)-vp.begin();
if(t<sq && vp[t].first==b){
if(i*sq+vp[t].second<d) return i*sq+vp[t].second;
}
b=matmul(b, ap);
}
return -1;
}
ll inv(ll a, ll m){
ll b=m, x=1, y=0;
while(b>0){
ll t=a/b;
swap(a-=t*b, b);
swap(x-=t*y, y);
}
return (x%m+m)%m;
}
int main()
{
cin>>p;
Mat a, b;
cin>>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1]>>b[0][0]>>b[0][1]>>b[1][0]>>b[1][1];
if(a==b){
cout<<1<<endl;
return 0;
}
Mat a2=matmul(a, a);
if(a2==b){
cout<<2<<endl;
return 0;
}
if(a2[0][0]==0 && a2[0][1]==0 && a2[1][0]==0 && a2[1][1]==0){
cout<<-1<<endl;
return 0;
}
if(a[0][0]*a[1][1]%p==a[0][1]*a[1][0]%p){
e=matpow(a, p-1);
}else{
e[0][0]=e[1][1]=1;
}
vector<ll> vd;
if(matpow(a, (p-1)*(p+1))!=e){
vd.push_back(p);
vd.push_back(p-1);
}else{
vd.push_back((p-1)/2);
vd.push_back((p+1)/2);
ll p2=4;
while(vd[0]%2==0){
p2<<=1;
vd[0]>>=1;
}
while(vd[1]%2==0){
p2<<=1;
vd[1]>>=1;
}
vd.push_back(p2);
}
ll d=1;
for(auto x:vd) d*=x;
//cout<<d<<endl;
int k=vd.size();
vector<ll> vf(k);
for(int i=0; i<k; i++){
ll x=vd[i];
vf[i]=discretelog(matpow(a, d/x), matpow(b, d/x), x);
if(vf[i]==-1){
cout<<-1<<endl;
return 0;
}
}
ll s=vf[0], q=vd[0];
for(int i=1; i<k; i++){
ll c=(vf[i]-s%vd[i]+vd[i])%vd[i]*inv(q%vd[i], vd[i])%vd[i];
s+=c*q;
q*=vd[i];
}
ll ans=(s==0 ? q : s);
if(matpow(a, ans)!=b) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
chocorusk