結果

問題 No.500 階乗電卓
ユーザー teketeke5000
提出日時 2017-08-25 12:58:41
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 1,615 ms
コンパイル使用メモリ 159,340 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 03:04:29
合計ジャッジ時間 2,363 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

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

ll MOD = 1e12;
bool over = false;

ll fact(ll n) {
    ll memo;
    if (n == 1) return 1;
    else {
        memo = n * fact(n-1);
        if (memo > MOD) {
            over = true;
            memo %= MOD;
            if (memo == 0) return 0;
        }
        return memo;
    }
}

int getDigit(ll n) {
    return to_string(n).length();
}

int main(void) {
    ll N;
    cin >> N;
    ll ans;
    if (N < 50) ans = fact(N);
    else {
        ans = 0;
        over = true;
    }
    if (!over) cout << ans << endl;
    else {
        for (int i=0; i<12 - getDigit(ans); i++) cout << 0;
        cout << ans << endl;
    }
    return 0;
}
0