結果

問題 No.950 行列累乗
ユーザー chocoruskchocorusk
提出日時 2019-11-26 01:25:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,561 bytes
コンパイル時間 1,600 ms
コンパイル使用メモリ 133,396 KB
実行使用メモリ 5,000 KB
最終ジャッジ日時 2023-08-08 05:08:49
合計ジャッジ時間 4,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
4,380 KB
testcase_01 AC 11 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 10 ms
4,380 KB
testcase_32 AC 21 ms
5,000 KB
testcase_33 AC 17 ms
4,384 KB
testcase_34 AC 12 ms
4,772 KB
testcase_35 AC 12 ms
4,508 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 16 ms
4,380 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 5 ms
4,380 KB
testcase_49 AC 12 ms
4,380 KB
testcase_50 AC 23 ms
4,832 KB
testcase_51 AC 35 ms
4,712 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 2 ms
4,384 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 1 ms
4,380 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 WA -
testcase_60 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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){
			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=order(a);
	if(vd.empty()){
		cout<<-1<<endl;
		return 0;
	}
	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];
	}
	if(s==0) cout<<q<<endl;
	else cout<<s<<endl;
	return 0;
}
0