結果

問題 No.950 行列累乗
ユーザー 👑 kmjpkmjp
提出日時 2019-12-13 00:14:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,611 bytes
コンパイル時間 1,867 ms
コンパイル使用メモリ 181,816 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-06-26 00:28:19
合計ジャッジ時間 5,595 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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];
	
	if(conv(A)==conv(B)) return _P("1\n");
	if((A.v[0][0]*A.v[1][1]-A.v[0][1]*A.v[1][0])%mo==0) return _P("-1\n");
	
	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;
	i=0;
	while(1) {
		if(M.count(conv(A2))) {
			cout<<i+M[conv(A2)]<<endl;
			return;
		}
		A2=mulmat(A2,QA);
		i+=j;
	}
	
	
}


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