結果

問題 No.950 行列累乗
ユーザー heno239heno239
提出日時 2019-12-13 04:51:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,028 bytes
コンパイル時間 2,804 ms
コンパイル使用メモリ 147,540 KB
実行使用メモリ 64,384 KB
最終ジャッジ日時 2024-06-26 06:59:40
合計ジャッジ時間 41,310 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 789 ms
64,256 KB
testcase_02 AC 223 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 223 ms
5,504 KB
testcase_06 AC 326 ms
5,376 KB
testcase_07 AC 211 ms
5,376 KB
testcase_08 AC 335 ms
5,376 KB
testcase_09 AC 243 ms
5,376 KB
testcase_10 AC 260 ms
5,376 KB
testcase_11 AC 217 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 374 ms
5,376 KB
testcase_14 AC 442 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 383 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 31 ms
6,016 KB
testcase_22 AC 1,501 ms
64,256 KB
testcase_23 AC 228 ms
5,504 KB
testcase_24 AC 1,529 ms
64,256 KB
testcase_25 AC 252 ms
5,376 KB
testcase_26 AC 239 ms
5,632 KB
testcase_27 AC 22 ms
5,376 KB
testcase_28 AC 1,498 ms
64,256 KB
testcase_29 AC 1,505 ms
64,384 KB
testcase_30 AC 1,499 ms
64,256 KB
testcase_31 AC 1,191 ms
64,384 KB
testcase_32 AC 1,161 ms
64,384 KB
testcase_33 AC 1,124 ms
64,256 KB
testcase_34 AC 1,163 ms
64,384 KB
testcase_35 AC 1,012 ms
64,384 KB
testcase_36 AC 865 ms
64,256 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 879 ms
64,256 KB
testcase_40 AC 245 ms
6,940 KB
testcase_41 AC 233 ms
6,940 KB
testcase_42 AC 32 ms
6,940 KB
testcase_43 AC 1,521 ms
64,384 KB
testcase_44 AC 1,495 ms
64,384 KB
testcase_45 AC 1,487 ms
64,256 KB
testcase_46 AC 1,499 ms
64,384 KB
testcase_47 AC 221 ms
6,940 KB
testcase_48 AC 1,524 ms
64,384 KB
testcase_49 WA -
testcase_50 AC 1,155 ms
64,256 KB
testcase_51 AC 1,093 ms
64,384 KB
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
testcase_56 WA -
testcase_57 AC 233 ms
6,940 KB
testcase_58 AC 219 ms
6,944 KB
testcase_59 AC 2 ms
6,940 KB
testcase_60 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define Rep(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) Rep(i,0,n)
#define stop char nyaa;cin>>nyaa;
using ll = long long;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
//const ll mod = 1e9 + 7;

using P = pair<int, int>;

using LP = pair<ll, ll>;

ll p;
typedef vector<vector<ll>> mat;
typedef vector<ll> vec;
mat mtmul(mat &A, mat &B) {
	mat C(A.size(), vec(B[0].size()));
	rep(i, (int)A.size()) {
		rep(k, (int)B.size()) {
			rep(j, (int)B[0].size()) {
				C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % p) % p;
			}
		}
	}
	return C;
}
mat mtpow(mat A, ll n) {
	mat B(A.size(), vec(A.size()));
	rep(i, (int)A.size()) {
		B[i][i] = 1;
	}
	while (n > 0) {
		if (n&(ll)1)B = mtmul(B, A);
		A = mtmul(A, A);
		n >>= 1;
	}
	return B;
}
ll dat(mat A) {
	ll res = A[0][0] * A[1][1] - A[0][1] * A[1][0];
	res = (res%p + p) % p;
	return res;
}

ll mod_pow(ll x, ll n) {
	ll res = 1;
	while (n > 0) {
		if (n % 2)res = res * x%p;
		x = x * x%p; n >>= 1;
	}
	return res;
}
mat invmt(mat A) {
	ll b = dat(A);
	mat res = { { A[1][1],p - A[0][1] },{ p - A[1][0],A[0][0] } };
	b = mod_pow(b, p - 2);
	rep(i, 2)rep(j, 2) {
		res[i][j] = res[i][j] * b%p;
	}
	return res;
}
//a^res=b (mod p)
ll ident(ll a, ll b) {
	if (b == 1)return p - 1;
	if (p <= 100000) {
		int tmp = 1;
		ll cop = a;
		while (cop != b) {
			cop = cop * a%p;
			tmp++;
			if (tmp > p)return -1;
		}
		return tmp;
	}
	ll d = sqrt(p);
	map<ll, int> mp;
	ll cop = b;
	ll inva = mod_pow(a, p - 2);
	for (int y = 0; y < d; y++) {
		mp[cop] = y;
		cop = cop * inva%p;
	}
	ll spea = mod_pow(a, d);
	cop = 1;
	for (int x = 0; x <= d; x++) {
		if (mp.find(cop) != mp.end()) {
			ll res = x * d + mp[cop];
			res %= (p - 1);
			return res;
		}
		cop = cop * spea%p;
	}
	return -1;
	assert(false);
	return 0;
}
ll minp(ll a) {
	vector<ll> ds;
	for (int i = 1; i <= 100000; i++) {
		if ((p - 1) % i == 0) {
			ds.push_back(i);
			ds.push_back((p - 1) / i);
		}
	}
	sort(ds.begin(), ds.end());
	ds.erase(unique(ds.begin(), ds.end()), ds.end());
	rep(i, ds.size()) {
		ll kata = ds[i];
		if (mod_pow(a, kata) == 1)return kata;
	}
	return p - 1;
}

void solve() {
	mat A(2), B(2);
	cin >> p;
	rep(i, 2) {
		A[i].resize(2);
		rep(j, 2)cin >> A[i][j];
	}
	rep(i, 2) {
		B[i].resize(2);
		rep(j, 2)cin >> B[i][j];
	}
	ll da = dat(A), db = dat(B);
	map<mat, bool> mp;
	if (da == 0) {
		if (db > 0) {
			cout << -1 << endl; return;
		}
		mat cop = A;
		if (cop == B) {
			cout << 1 << endl; return;
		}
		mp[cop] = true;
		int tmp = 1;
		while (true) {
			tmp++;
			cop = mtmul(cop, A);
			if (mp[cop]||tmp>300000) {
				cout << -1 << endl; return;
			}
			if (cop == B) {
				cout << tmp << endl; return;
			}
			mp[cop] = true;
		}
	}
	if (db == 0) {
		cout << -1 << endl; return;
	}
	ll k, e;
	if (da == 1) {
		if (db != 1) {
			cout << -1 << endl; return;
		}
		k = 1, e = 1;

	}
	else {
		k = ident(da, db);
		e = minp(da);
		if (k > 0) {
			k %= e;
			if (k == 0)k = e;
		}
	}

	if (k < 0) {
		cout << -1 << endl; return;
	}


	//cout << Z[0][0] << " " << Z[0][1] << " " << Z[1][0] << " " << Z[1][1] << endl;
	mat ap = mtpow(A, e);
	mat aa = mtpow(A, k); aa = invmt(aa);
	mat bp = B;
	bp = mtmul(bp, aa);
	int sz = 300000;
	map<mat, int> memo;
	ll ans = (ll)1 << 62;
	mat invap = invmt(ap);
	mat X = bp;
	for (int y = 0; y <= sz; y++) {
		if (memo.find(X) == memo.end()) {
			memo[X] = y;
		}
		X = mtmul(X, invap);
	}
	mat app = mtpow(ap, sz);
	X = { { 1,0 },{ 0,1 } };
	for (ll x = 0; x <= sz; x++) {
		if (memo.find(X) != memo.end()) {
			ll cs = x * sz + memo[X];
			ans = min(ans, cs);
		}
		X = mtmul(X, app);
	}
	if (ans == ((ll)1 << 62)) {
		cout << -1 << endl; return;
	}
	cout << ans * e + k << endl;
}


signed main() {
	solve();
	//stop
	return 0;
}
0