結果

問題 No.575 n! / m / m / m...
ユーザー minamiminami
提出日時 2019-04-04 19:09:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,592 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 173,252 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-26 22:00:15
合計ジャッジ時間 5,667 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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(1) で近似して計算できる
// 整数部 x と小数部 y に分けると、n! = y e^x として表せる
// ただし計算しようとするとオーバーフローする
double logFactorial(double n) {
	return n * log(n) - n + log(2 * acos(-1) * n) + 1.0 / (12 * 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;
	PrimeFactorization<long long> pf(n);
	auto factors = pf.factorize(m);
	long long div = LLONG_MAX;
	for (auto f : factors) {
		chmin(div, legendreFormula(n, f));
	}
	if (n < 100) {
		double fact = 1;
		long long d = 0;
		rep(i, 1, n + 1) {
			fact *= i;
			while (fact >= 10) {
				fact /= 10;
				d++;
			}
		}
		rep(i, 0, div) {
			fact /= m;
			while (fact < 1) {
				fact *= 10;
				d--;
			}
		}
		cout << fact << "e" << d << endl;
	}
	else {
		double r = logFactorial(n) / log(10);
		long long x = r;
		double y = r - x;
		y *= 10;
		x--;
		cout << y << "e" << x << endl;
	}
	return 0;
}
0