結果
| 問題 |
No.950 行列累乗
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2019-12-11 15:00:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,526 bytes |
| コンパイル時間 | 1,500 ms |
| コンパイル使用メモリ | 135,392 KB |
| 実行使用メモリ | 13,756 KB |
| 最終ジャッジ日時 | 2024-06-24 07:41:06 |
| 合計ジャッジ時間 | 5,356 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 TLE * 1 |
| other | AC * 2 -- * 55 |
ソースコード
#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;
vector<P> fac(bool myon){
map<ll, int> mp;
vector<P> ret;
ll x=p-1;
for(ll i=2; i*i<=x; i++){
if(x%i==0){
int f=0;
while(x%i==0){
x/=i;
f++;
}
mp[i]+=f;
}
}
if(x>1) mp[x]++;
if(!myon){
for(auto q:mp) ret.push_back(q);
return ret;
}
x=p+1;
for(ll i=2; i*i<=x; i++){
if(x%i==0){
int f=0;
while(x%i==0){
x/=i;
f++;
}
mp[i]+=f;
}
}
if(x>1) mp[x]++;
for(auto q:mp) ret.push_back(q);
return ret;
}
vector<ll> order(Mat a){
ll x=(p-1)*(p+1);
bool myon=0;
if(matpow(a, x)!=e){
x=p*(p-1);
}else{
myon=1;
}
vector<P> v=fac(myon);
vector<ll> ret;
for(auto q:v){
ll r=q.first; int f=q.second;
ll y=1;
for(int i=0; i<f; i++) y*=r;
for(int i=0; i<f; i++){
x/=r, y/=r;
if(matpow(a, x)!=e){
x*=r;
y*=r;
break;
}
}
if(y>1) ret.push_back(y);
}
if(!myon) ret.push_back(p);
return ret;
}
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;
}
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=order(a);
if(vd.empty()){
cout<<-1<<endl;
return 0;
}
ll d=1;
for(auto x:vd) d*=x;
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++){
for(ll j=s; ; j+=vd[i]){
if(j%vd[i]==vf[i]){
s=j;
q*=vd[i];
break;
}
}
}
ll ans=(s==0 ? q : s);
if(matpow(a, ans)!=b) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
chocorusk