結果

問題 No.502 階乗を計算するだけ
ユーザー Kmcode1Kmcode1
提出日時 2017-04-07 23:36:43
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,648 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 143,844 KB
実行使用メモリ 8,756 KB
最終ジャッジ日時 2023-09-23 02:55:54
合計ジャッジ時間 6,489 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,756 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 6 ms
4,376 KB
testcase_31 AC 4 ms
4,380 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;




#define MOD 1000000007

#define ull unsigned long long int

template<ull mod>
struct fast_fact {
	ull m(ull a, ull b) const {
		ull r = (a*b) % mod;
		return r;
	}
	template<class...Ts>
	ull m(ull a, ull b, Ts...ts) const {
		return m(m(a, b), ts...);
	}
	// calculates x!!, ie 1*3*5*7*...
	ull double_fact(ull x) const {
		ull ret = 1;
		for (ull i = 3; i < x; i += 2) {
			ret = m(i, ret);
		}
		return ret;
	}
	// calculate 2^2^n for n=0...bits in ull
	// a pointer to this is stored statically to make calculating
	// 2^k faster:
	ull const* get_pows() const {
		static ull retval[sizeof(ull) * 8] = { 2 % mod };
		for (int i = 1; i < sizeof(ull) * 8; ++i) {
			retval[i] = m(retval[i - 1], retval[i - 1]);
		}
		return retval;
	}
	// calculate 2^x.  We decompose x into bits
	// and multiply together the 2^2^i for each bit i
	// that is set in x:
	ull pow_2(ull x) const {
		static ull const* pows = get_pows();
		ull retval = 1;
		for (int i = 0; x; ++i, (x = x / 2)){
			if (x & 1) retval = m(retval, pows[i]);
		}
		return retval;
	}
	// the actual calculation:
	ull operator()(ull x) const {
		x = x%mod;
		if (x == 0) return 1;
		ull result = 1;
		// odd case:
		if (x & 1) result = m((*this)(x - 1), x);
		else result = m(double_fact(x), pow_2(x / 2), (*this)(x / 2));
		return result;
	}
};
template<ull mod>
ull factorial_mod(ull x) {
	return fast_fact<mod>()(x);
}

int main(){
	long long int n;
	scanf("%lld", &n);
	if (n >= MOD){
		puts("0");
		return 0;
	}
	long long int c = factorial_mod<MOD>(n);
	printf("%lld\n", c);
	return 0;
}
0