結果

問題 No.575 n! / m / m / m...
ユーザー minamiminami
提出日時 2019-04-04 19:41:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,597 bytes
コンパイル時間 1,902 ms
コンパイル使用メモリ 174,092 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 22:35:11
合計ジャッジ時間 3,706 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 WA -
testcase_23 AC 10 ms
4,376 KB
testcase_24 WA -
testcase_25 AC 10 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

// 階乗の対数表現
// スターリングの近似を用いて
// ln n! を O(log()の計算量) で近似して計算できる
// 整数部 x と小数部 y に分けると、n! = y e^x として表せる
// ただしそれを計算しようとすると容易にオーバーフローする
// n が小さければ愚直に計算する
const double PI = acos(-1);
double logFactorial(long long n) {
	if (n < 100) {
		double ret = 0;
		for (long long i = 1; i <= n; i++) 
			ret += log(i);
		return ret;
	}
	return n * log(n) - n + log(2.0 * PI * n) / 2.0 + 1.0 / (12.0 * n);
}

// n! を素因数 p で最大何回割り切れるか
long long legendreFormula(long long n, long long p) {
	long long ret = 0;
	long long q = p;
	while (q <= n) {
		ret += n / q;
		q *= p;
	}
	return ret;
}

template<typename T>
struct PrimeFactorization {
	T max_n;
	vector<bool> is_prime;
	vector<T> primes;
	// 前処理
	PrimeFactorization(T max_n) :max_n(max_n) {
		T s = sqrt(max_n);
		is_prime.assign(s + 1, true);
		is_prime[0] = is_prime[1] = false;
		for (T i = 2; i*i <= s; i++) {
			if (!is_prime[i])continue;
			for (T j = i * i; j <= s; j += i)
				is_prime[j] = false;
		}
		for (T i = 0; i <= s; i++)
			if (is_prime[i])
				primes.emplace_back(i);
	}
	// 昇順で素因数を返す
	// √n以下の素数に対して割り切れるか調べる
	vector<T> factorize(T n) {
		assert(n >= 2);
		vector<T> factors;
		for (auto &p : primes) {
			while (n%p == 0) {
				n /= p;
				factors.emplace_back(p);
			}
		}
		if (n != 1)factors.emplace_back(n);
		return factors;
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	long long n, m; cin >> n >> m;
	cout << fixed << setprecision(10);
	PrimeFactorization<long long> pf(n);
	auto factors = pf.factorize(m);
	long long d = LLONG_MAX;
	for (auto f : factors) {
		chmin(d, legendreFormula(n, f));
	}
	double r = logFactorial(n) - log(m) * d;
	r /= log(10);
	long long x = r;
	double y = r - x;
	y = pow(10, y);
	cout << y << "e" << x << endl;
	return 0;
}
0