結果

問題 No.950 行列累乗
ユーザー 👑 kmjpkmjp
提出日時 2019-12-13 02:00:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,207 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 183,376 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-26 03:56:01
合計ジャッジ時間 6,929 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 RE -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 RE -
testcase_33 WA -
testcase_34 RE -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
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 WA -
testcase_59 RE -
testcase_60 RE -
権限があれば一括ダウンロードができます

ソースコード

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;
}

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];
	
	vector<ll> V[2];
	for(i=1;1LL*i*i<=mo-1;i++) if((mo-1)%i==0) {
		V[0].push_back(i);
		if(i*i!=mo-1) V[0].push_back((mo-1)/i);
	}
	if(conv(powmat(mo*(mo-1)+1,A))==conv(A)) {
		V[1].push_back(1);
		V[1].push_back(mo);
	}
	else if(conv(powmat((mo+1)*(mo-1)+1,A))==conv(A)) {
		for(i=1;1LL*i*i<=mo+1;i++) if((mo+1)%i==0) {
			V[1].push_back(i);
			if(i*i!=mo+1) V[1].push_back((mo+1)/i);
		}
		
	}
	else {
		assert(0);
	}
	
	sort(ALL(V[0]));
	sort(ALL(V[1]));
	
	ll per=(mo-1)*(mo+1);
	FORR(a,V[0]) FORR(b,V[1]) if(a*b>1 && a*b<per) {
		Mat E3=powmat(a*b+1,A);
		if(conv(E3)==conv(A)) per=a*b;
	}
	
	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