結果

問題 No.2487 Multiple of M
ユーザー square1001square1001
提出日時 2023-09-30 00:08:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,199 bytes
コンパイル時間 2,248 ms
コンパイル使用メモリ 212,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 12:50:16
合計ジャッジ時間 4,145 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template <std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

using mint = modint<998244353>;

long long mygcd(long long x, long long y) {
	if (y == 0) {
		return x;
	}
	return mygcd(y, x % y);
}

array<mint, 2> merge(long long X, const array<mint, 2>& v1, const array<mint, 2>& v2) {
	array<mint, 2> res = { mint(0), mint(0) };
	res[1] += v1[1] * v2[1];
	res[0] += v1[0] * v2[1];
	res[0] += v1[1] * v2[0];
	res[0] += v1[0] * v2[0] * (X - 2);
	res[1] += v1[0] * v2[0] * (X - 1);
	return res;
}

mint solve(long long N, long long M, long long K) {
	long long cur = 1;
	vector<long long> seq = { 1 };
	while (true) {
		cur = mygcd(cur * K, M);
		if (cur == seq.back()) {
			break;
		}
		seq.push_back(cur);
	}
	if (seq.size() > N) {
		seq.resize(N);
	}
	int V = seq.size();
	seq.push_back(M);
	vector<long long> d(V);
	for (int i = 0; i < V; i++) {
		d[i] = seq[i + 1] / seq[i];
	}
	vector<vector<mint> > dp(V);
	dp[0] = { mint(1) };
	for (int i = 1; i < V; i++) {
		dp[i].resize(i + 1);
		for (int j = 0; j < i; j++) {
			dp[i][j + 1] += dp[i - 1][j] * seq[i - 1];
			dp[i][j] += dp[i - 1][j];
		}
		for (int j = 0; j <= i; j++) {
			dp[i][j] *= (j >= 1 ? mint(d[i - 1]).pow(j - 1) : mint(1));
		}
	}
	array<mint, 2> ini = { mint(0), mint(0) };
	for (int i = 0; i < V; i++) {
		array<mint, 2> sub;
		sub[0] = dp[V - 1][i] * (i >= 1 ? mint(d[V - 1]).pow(i - 1) : mint(0));
		sub[1] = dp[V - 1][i] * (i >= 1 ? mint(d[V - 1]).pow(i - 1) : mint(1));
		if ((V - i - 1) % 2 == 0) {
			ini[0] += sub[0];
			ini[1] += sub[1];
		}
		else {
			ini[0] -= sub[0];
			ini[1] -= sub[1];
		}
	}
	array<mint, 2> g = { seq[V - 1], seq[V - 1] - 1 };
	array<mint, 2> v = { mint(0), mint(1) };
	long long X = M / seq[V - 1];
	long long rem = N - V + 1;
	while (rem >= 1) {
		if (rem % 2 == 1) {
			v = merge(X, g, v);
		}
		g = merge(X, g, g);
		rem /= 2;
	}
	array<mint, 2> ans = merge(X, ini, v);
	return ans[1];
}

int main() {
	long long N, M, K;
	cin >> N >> M >> K;
	mint ans = solve(N, M, K);
	cout << ans.get() << endl;
	return 0;
}
0