結果

問題 No.1648 Sum of Powers
ユーザー kwm_tkwm_t
提出日時 2021-08-14 03:08:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 2,966 bytes
コンパイル時間 4,023 ms
コンパイル使用メモリ 239,100 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-15 00:43:42
合計ジャッジ時間 8,530 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
12,992 KB
testcase_01 AC 5 ms
6,944 KB
testcase_02 AC 41 ms
11,848 KB
testcase_03 AC 42 ms
11,904 KB
testcase_04 AC 45 ms
11,844 KB
testcase_05 AC 42 ms
11,904 KB
testcase_06 AC 43 ms
11,904 KB
testcase_07 AC 41 ms
11,972 KB
testcase_08 AC 44 ms
11,904 KB
testcase_09 AC 41 ms
11,848 KB
testcase_10 AC 41 ms
11,972 KB
testcase_11 AC 41 ms
11,844 KB
testcase_12 AC 45 ms
11,904 KB
testcase_13 AC 42 ms
11,904 KB
testcase_14 AC 42 ms
11,904 KB
testcase_15 AC 42 ms
11,776 KB
testcase_16 AC 42 ms
11,904 KB
testcase_17 AC 43 ms
11,776 KB
testcase_18 AC 41 ms
11,848 KB
testcase_19 AC 40 ms
11,844 KB
testcase_20 AC 41 ms
11,904 KB
testcase_21 AC 40 ms
12,032 KB
testcase_22 AC 50 ms
12,996 KB
testcase_23 AC 48 ms
12,992 KB
testcase_24 AC 48 ms
12,996 KB
testcase_25 AC 47 ms
12,928 KB
testcase_26 AC 46 ms
12,928 KB
testcase_27 AC 46 ms
12,868 KB
testcase_28 AC 49 ms
12,864 KB
testcase_29 AC 50 ms
12,868 KB
testcase_30 AC 50 ms
12,800 KB
testcase_31 AC 53 ms
12,800 KB
testcase_32 AC 50 ms
12,800 KB
testcase_33 AC 49 ms
12,800 KB
testcase_34 AC 49 ms
12,872 KB
testcase_35 AC 46 ms
12,800 KB
testcase_36 AC 50 ms
13,056 KB
testcase_37 AC 54 ms
12,928 KB
testcase_38 AC 51 ms
12,928 KB
testcase_39 AC 59 ms
12,928 KB
testcase_40 AC 51 ms
12,800 KB
testcase_41 AC 60 ms
12,928 KB
testcase_42 AC 51 ms
13,000 KB
testcase_43 AC 52 ms
12,928 KB
testcase_44 AC 50 ms
12,996 KB
testcase_45 AC 52 ms
12,800 KB
testcase_46 AC 50 ms
13,056 KB
testcase_47 AC 44 ms
11,776 KB
testcase_48 AC 6 ms
6,940 KB
testcase_49 AC 42 ms
11,776 KB
testcase_50 AC 47 ms
11,972 KB
testcase_51 AC 5 ms
7,108 KB
testcase_52 AC 49 ms
12,800 KB
testcase_53 AC 49 ms
12,868 KB
testcase_54 AC 45 ms
12,996 KB
testcase_55 AC 50 ms
12,864 KB
testcase_56 AC 50 ms
12,800 KB
testcase_57 AC 5 ms
7,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#include "atcoder/all"
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
using mint = modint998244353;
const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
//const bool debug = false;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
mint pm[100001][2][2];
mint pm2[100001][2][2];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	long long a, b, p, q; cin >> a >> b >> p >> q;
	if (0 == b) {
		// pre clac
		vector<mint>pwa(100000 + 1), pwa2(100000 + 1);
		pwa[0] = 1; pwa2[0] = 1;
		map<int, int>mp;
		mp[0] = 0;
		mint memo = pow_mod(a, 100000, mod);//a^(10^5)
		rep(i, 100000) {
			pwa[i + 1] = pwa[i] * a;
			mp[pwa[i + 1].val()] = i + 1;
			pwa2[i + 1] = pwa2[i] * memo;
		}
		// x = 0, y = a
		// a^ n = p となる n
		// n = 10^5 * v + w;
		// v : fix
		rep(i, 100001) {
			mint aw = (mint)p * pwa2[i].inv();
			if (mp.count(aw.val())) {
				long long ans = 100000LL * i + mp[aw.val()];
				if (1 == ans)continue;
				cout << ans << endl;
				return 0;
			}
		}
	}
	else {
		pm[0][0][0] = 1;
		pm[0][1][1] = 1;
		rep(i, 100000) {
			pm[i + 1][0][0] = pm[i][0][0] * a + pm[i][0][1];
			pm[i + 1][0][1] = pm[i][0][0] * (-b);
			pm[i + 1][1][0] = pm[i][1][0] * a + pm[i][1][1];
			pm[i + 1][1][1] = pm[i][1][0] * (-b);
		}
		mint ab = (mint)a * ((mint)b).inv();
		map<P, int>mp;
		rep(i, 100001) {
			mint np = pm[i][0][0] * 2 + pm[i][0][1] * ab;
			mint nq = pm[i][1][0] * 2 + pm[i][1][1] * ab;
			mp[{np.val(), nq.val()}] = i;
		}
		pm2[0][0][0] = 1;
		pm2[0][1][1] = 1;
		mint s, t, u, v, invdet;
		invdet = pm[100000][0][0] * pm[100000][1][1] - pm[100000][0][1] * pm[100000][1][0];
		invdet = invdet.inv();
		s = pm[100000][1][1] * invdet;
		t = pm[100000][0][1] * -1 * invdet;
		u = pm[100000][1][0] * -1 * invdet;
		v = pm[100000][0][0] * invdet;
		rep(i, 100000) {
			pm2[i + 1][0][0] = pm2[i][0][0] * s + pm2[i][0][1] * u;
			pm2[i + 1][0][1] = pm2[i][0][0] * t + pm2[i][0][1] * v;
			pm2[i + 1][1][0] = pm2[i][1][0] * s + pm2[i][1][1] * u;
			pm2[i + 1][1][1] = pm2[i][1][0] * t + pm2[i][1][1] * v;
		}
		rep(i, 100001) {
			mint np = pm2[i][0][0] * p + pm2[i][0][1] * q;
			mint nq = pm2[i][1][0] * p + pm2[i][1][1] * q;
			if (mp.count({ np.val(),nq.val() })) {
				long long ans = 100000LL * i + mp[{ np.val(), nq.val() }];
				if (1 == ans)continue;
				cout << ans << endl;
				return 0;
			}
		}
	}
	return 0;
}
0