結果

問題 No.502 階乗を計算するだけ
ユーザー Kmcode1Kmcode1
提出日時 2017-04-07 23:30:53
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,207 bytes
コンパイル時間 1,276 ms
コンパイル使用メモリ 152,824 KB
実行使用メモリ 19,400 KB
最終ジャッジ日時 2023-09-23 02:47:58
合計ジャッジ時間 13,668 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
19,144 KB
testcase_01 AC 142 ms
19,188 KB
testcase_02 AC 142 ms
19,268 KB
testcase_03 AC 142 ms
19,192 KB
testcase_04 AC 142 ms
19,144 KB
testcase_05 AC 142 ms
19,128 KB
testcase_06 AC 142 ms
19,060 KB
testcase_07 AC 142 ms
19,268 KB
testcase_08 AC 142 ms
19,400 KB
testcase_09 AC 142 ms
19,208 KB
testcase_10 AC 142 ms
19,360 KB
testcase_11 AC 142 ms
19,204 KB
testcase_12 AC 143 ms
19,272 KB
testcase_13 AC 143 ms
19,068 KB
testcase_14 AC 142 ms
19,140 KB
testcase_15 AC 142 ms
19,128 KB
testcase_16 AC 142 ms
19,140 KB
testcase_17 AC 142 ms
19,148 KB
testcase_18 AC 142 ms
19,132 KB
testcase_19 AC 142 ms
19,208 KB
testcase_20 AC 143 ms
19,268 KB
testcase_21 AC 142 ms
19,132 KB
testcase_22 AC 142 ms
19,392 KB
testcase_23 AC 142 ms
19,196 KB
testcase_24 AC 142 ms
19,144 KB
testcase_25 AC 143 ms
19,132 KB
testcase_26 AC 142 ms
19,072 KB
testcase_27 AC 142 ms
19,144 KB
testcase_28 AC 143 ms
19,156 KB
testcase_29 AC 142 ms
19,136 KB
testcase_30 AC 143 ms
19,072 KB
testcase_31 AC 143 ms
19,148 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 AC 143 ms
19,208 KB
testcase_43 AC 142 ms
19,084 KB
testcase_44 AC 142 ms
19,076 KB
testcase_45 AC 142 ms
19,220 KB
testcase_46 AC 142 ms
18,992 KB
testcase_47 AC 141 ms
19,068 KB
testcase_48 AC 142 ms
19,064 KB
testcase_49 AC 141 ms
19,072 KB
testcase_50 AC 142 ms
19,080 KB
testcase_51 AC 142 ms
19,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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




#define MOD 1000000007

map<pair<long long int,long long int>, long long int> mp;

#define M 1000002
long long int F[M];
long long int ppow(long long int i, long long int j){
	long long int res = 1;
	while (j){
		if(j&1)res *= i;
		if (res >= MOD)res %= MOD;
		i *= i;
		i %= MOD;
		j >>= 1;
	}
	return res;
}
long long int inv[M];

long long int recfact(long long int start, long long int n) {
	long long int i;
	if (mp.count(make_pair(start, n) )){
		return mp[make_pair(start, n)];
	}
	if (n <= M) {
		long long int r = inv[start]*F[start+n-1];
		r %= MOD;
		//for (i = start + 1; i < start + n; i++) r *= i,r%=MOD;
		return r%MOD;
	}
	i = n / 2;
	mp[make_pair(start,n)]=(recfact(start, i) * recfact(start + i, n - i))%MOD;
	return mp[{start, n}];
}
long long int factorial(long  n) { return recfact(1, n); }

int main(){
	F[0] = 1;
	inv[0] = 1;
	for (int i = 1; i < M; i++){
		F[i] = F[i - 1];
		F[i] *= i;
		F[i] %= MOD;
		inv[i] = ppow(F[i], MOD - 1);
	}
	long long int n;
	scanf("%lld", &n);
	if (n >= MOD){
		puts("0");
		return 0;
	}
	long long int c = factorial(n);
	printf("%lld\n", c);
	return 0;
}
0