結果

問題 No.502 階乗を計算するだけ
ユーザー Kmcode1
提出日時 2017-04-07 23:30:53
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,207 bytes
コンパイル時間 1,500 ms
コンパイル使用メモリ 168,164 KB
実行使用メモリ 19,340 KB
最終ジャッジ日時 2024-07-16 03:09:04
合計ジャッジ時間 13,327 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 42 RE * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:55:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   55 |         scanf("%lld", &n);
      |         ~~~~~^~~~~~~~~~~~

ソースコード

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