結果

問題 No.575 n! / m / m / m...
ユーザー parukiparuki
提出日時 2017-10-13 12:58:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,910 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 165,744 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 00:18:59
合計ジャッジ時間 3,329 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

/*
Legendre's formula

nを自然数、pを素数とするとき
p^x | n!
を満たす最大の非負整数xを返す。
時間
O(log(n))

参考
https://en.wikipedia.org/wiki/Legendre%27s_formula
*/
long long LegendreFormula(long long n, long long p) {
    long long res = 0, d = p, a;
    while ((a = n / d) != 0) {
        res += a;
        d *= p;
    }
    return res;
}

/*
スターリングの近似
log(n!)の近似
https://ja.wikipedia.org/wiki/%E3%82%B9%E3%82%BF%E3%83%BC%E3%83%AA%E3%83%B3%E3%82%B0%E3%81%AE%E8%BF%91%E4%BC%BC
*/
double stirlingsApproximation(long long n) {
    return n * log(n) - n + 0.5*log(2 * M_PI*n) + 1.0 / (12.0*n);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(10);
    ll n, m, p;
    cin >> n >> m;
    ll M = m;
    double x = stirlingsApproximation(n);

    ll divnum = LLONG_MAX;
    auto f = [&](ll p, int cnt) {
        smin(divnum, LegendreFormula(n, p) / cnt);
    };
    for (p = 2; p*p <= m; ++p) {
        if (m%p == 0) {
            int cnt = 0;
            while (m%p == 0)m /= p, ++cnt;
            f(p, cnt);
        }
    }

    if (m != 1) {
        f(m, 1);
    }
    x -= divnum * log(M);
    double x10 = x / log(10.0);
    ll pw = (ll)x10;
    x10 -= pw;
    x = x10 * log(10);
    cout << exp(x) << 'e' << pw << endl;
}
0