結果

問題 No.950 行列累乗
ユーザー 👑 kmjpkmjp
提出日時 2019-12-14 11:40:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,905 bytes
コンパイル時間 2,020 ms
コンパイル使用メモリ 187,148 KB
実行使用メモリ 5,572 KB
最終ジャッジ日時 2023-09-10 11:49:35
合計ジャッジ時間 11,128 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll mo;
const int MAT=2;
struct Mat { ll v[MAT][MAT]; Mat(){ZERO(v);};};

Mat mulmat(Mat& a,Mat& b,int n=MAT) {
	ll mo2=4*mo*mo;
	int x,y,z; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(x,n) FOR(z,n) FOR(y,n) {
		r.v[x][y] += a.v[x][z]*b.v[z][y];
		if(r.v[x][y]>mo2) r.v[x][y] -= mo2;
	}
	FOR(x,n) FOR(y,n) r.v[x][y]%=mo;
	return r;
}

Mat powmat(ll p,Mat a,int n=MAT) {
	int i,x,y; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(i,n) r.v[i][i]=1;
	while(p) {
		if(p%2) r=mulmat(r,a,n);
		a=mulmat(a,a,n);
		p>>=1;
	}
	return r;
}

ll modpow(ll a, ll n = mo-2) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

Mat revmat(Mat a,int n=MAT) {
	Mat m;
	int x,y,z;
	FOR(x,n) FOR(y,n) m.v[x][y]=x==y;
	
	FOR(x,n) {
		for(y=x;y<n;y++) if(a.v[y][x]) break;
		assert(y!=n);
		FOR(z,n) swap(a.v[x][z],a.v[y][z]),swap(m.v[x][z],m.v[y][z]);
		ll r=modpow(a.v[x][x]);
		FOR(y,n) (a.v[x][y]*=r)%=mo,(m.v[x][y]*=r)%=mo;
		FOR(y,n) if(y!=x) {
			r = a.v[y][x];
			FOR(z,n) {
				m.v[y][z] -= r*m.v[x][z]%mo;
				if(m.v[y][z]<0) m.v[y][z]+=mo;
				a.v[y][z] -= r*a.v[x][z]%mo;
				if(a.v[y][z]<0) a.v[y][z]+=mo;
			}
		}
	}
	return m;
	
}
Mat A,B;

vector<ll> conv(Mat M) {
	vector<ll> V;
	V.push_back(M.v[0][0]);
	V.push_back(M.v[0][1]);
	V.push_back(M.v[1][0]);
	V.push_back(M.v[1][1]);
	return V;
}


ll modlog(int g,int a) {  // return g^x=a mod a
	map<int,int> M;
	ll cur=1;
	int sq=sqrt(mo);
	int i;
	FOR(i,sq) {
		M[cur]=i;
		cur=cur*g%mo;
	}
	
	ll step=cur;
	cur=1;
	ll num=0;
	int lp=0;
	while(1) {
		ll x=a*modpow(cur)%mo;
		if(lp>sq+5) return 1LL<<50;
		if(M.count(x)) return num+M[x];
		lp++;
		cur=cur*step%mo;
		num+=sq;
	}
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>mo;
	cin>>A.v[0][0]>>A.v[0][1];
	cin>>A.v[1][0]>>A.v[1][1];
	cin>>B.v[0][0]>>B.v[0][1];
	cin>>B.v[1][0]>>B.v[1][1];
	
	ll DA=((A.v[0][0]*A.v[1][1]-A.v[0][1]*A.v[1][0])%mo+mo)%mo;
	ll DB=((B.v[0][0]*B.v[1][1]-B.v[0][1]*B.v[1][0])%mo+mo)%mo;
	
	if((DA==0) != (DB==0)) return _P("-1\n");
	
	if(DA==0) {
		//周期はP-1以内なので普通にBSGS
		Mat EE;
		EE.v[0][0]=EE.v[1][1]=1;
		
		
		Mat RA=revmat(A);
		Mat E=B,QA;
		QA.v[0][0]=QA.v[1][1]=1;
		map<vector<ll>,int> M;
		M[conv(E)]=0;
		
		for(i=1;1LL*i*i<=mo;i++) {
			E=mulmat(RA,E);
			M[conv(E)]=i;
			QA=mulmat(QA,A);
		}
		j=i-1;
		Mat A2;
		A2.v[0][0]=A2.v[1][1]=1;
		ll P=0;
		ll ret=1LL<<60;
		while(P<=mo) {
			if(M.count(conv(A2))) ret=min(ret,(P+M[conv(A2)]));
			A2=mulmat(A2,QA);
			P+=j;
		}
		
		if(ret==1LL<<60) ret=-1;
		cout<<ret<<endl;
		return;
	}
	
	ll R=modlog(DA,DB);
	if(R>1LL<<40) return _P("-1\n");
	
	//位数を求める
	ll per=mo;
	for(i=1;1LL*i*i<=mo;i++) if((mo-1)%i==0) {
		if(modpow(DA,i)==1) per=min(per,(ll)i);
		if(modpow(DA,(mo-1)%i)==1) per=min(per,(mo-1)%i);
	}
	
	assert(0);
	return;
	
	Mat EE;
	EE.v[0][0]=EE.v[1][1]=1;
	
	
	Mat RA=revmat(A);
	Mat E=B,QA;
	QA.v[0][0]=QA.v[1][1]=1;
	map<vector<ll>,int> M;
	M[conv(E)]=0;
	
	for(i=1;1LL*i*i<=mo;i++) {
		E=mulmat(RA,E);
		M[conv(E)]=i;
		QA=mulmat(QA,A);
	}
	j=i-1;
	Mat A2;
	A2.v[0][0]=A2.v[1][1]=1;
	ll P=0;
	ll ret=1LL<<60;
	while(P<=mo) {
		if(M.count(conv(A2))) {
			ret=min(ret,(P+M[conv(A2)])%per);
		}
		A2=mulmat(A2,QA);
		P+=j;
	}
	
	if(ret==1LL<<60) ret=-1;
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0