結果

問題 No.1809 Divide NCK
ユーザー MasKoaTS
提出日時 2022-01-12 18:40:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 939 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 75,084 KB
最終ジャッジ日時 2025-01-27 10:35:51
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const ll inf = 1000000000000000000;


VV<ll> prime_factorize(ll n) {
	VV<ll> prime = {};
	ll f = 2;
	while (f * f <= n) {
		if (n % f == 0) {
			n /= f;
			ll cnt = 1;
			while (n % f == 0) {
				n /= f;
				cnt++;
			}
			prime.push_back({ f,cnt });
		}
		f++;
	}
	if (n != 1) {
		prime.push_back({ n,1 });
	}
	return prime;
}

ll digit_sum(ll n, ll p) {
	ll res = 0;
	while (n) {
		res += n % p;
		n /= p;
	}
	return res;
}

ll kummer(ll N, ll K, V<ll>& p) {
	ll cnt = (digit_sum(N - K, p[0]) + digit_sum(K, p[0]) - digit_sum(N, p[0])) / (p[0] - 1);
	return cnt / p[1];
}

int main(void) {
	ll N, K, M;		cin >> N >> K >> M;

	VV<ll> prime = prime_factorize(M);
	ll ans = inf;

	for (auto& p : prime) {
		ans = min(ans, kummer(N, K, p));
	}

	cout << ans << endl;
	return 0;
}
0