結果

問題 No.950 行列累乗
ユーザー 👑 kmjpkmjp
提出日時 2019-12-14 12:18:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,321 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 193,756 KB
実行使用メモリ 11,596 KB
最終ジャッジ日時 2023-09-10 11:50:58
合計ジャッジ時間 8,592 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 55 ms
5,444 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 72 ms
8,784 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 39 ms
4,872 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 80 ms
9,172 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 72 ms
9,340 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 57 ms
5,476 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 120 ms
11,280 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 2 ms
4,376 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) {
		assert(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");
	
	//detAの位数を求める
	ll P=mo;
	for(i=1;1LL*i*i<=mo;i++) if((mo-1)%i==0) {
		if(modpow(DA,i)==1) P=min(P,(ll)i);
		if(modpow(DA,(mo-1)/i)==1) P=min(P,(mo-1)/i);
	}
	
	// Aの位数を求める
	vector<ll> V[3];
	FOR(i,3) {
		ll T=mo+1-i;
		for(j=1;j*j<=T;j++) if(T%j==0) V[i].push_back(j),V[i].push_back(T/j);
	}
	ll TP=mo*mo;
	FORR(x,V[0]) FORR(y,V[1]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y);
	FORR(x,V[0]) FORR(y,V[2]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y);
	FORR(x,V[1]) FORR(y,V[2]) if(conv(powmat(x*y+1,A))==conv(A)) TP=min(TP,x*y);
	
	//cerr<<R<<" "<<P<<endl;
	Mat PA=powmat(P,A);
	Mat revA=revmat(PA);
	Mat E=B,QPA;

	QPA.v[0][0]=QPA.v[1][1]=1;
	map<vector<ll>,ll> M;
	M[conv(E)]=0;
	
	for(i=1;1LL*i*i<=2*mo;i++) {
		E=mulmat(revA,E);
		M[conv(E)]=i*P;
		QPA=mulmat(QPA,PA);
	}
	j=i-1;
	Mat A2=powmat(R,A);
	ll count=R;
	ll ret=1LL<<60;
	while(count+2*j<=2*mo) {
		if(M.count(conv(A2))) {
			ret=min(ret,(count*P+M[conv(A2)])%TP);
		}
		A2=mulmat(A2,QPA);
		count+=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