結果
| 問題 |
No.502 階乗を計算するだけ
|
| コンテスト | |
| ユーザー |
Kmcode1
|
| 提出日時 | 2017-04-07 23:36:43 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,648 bytes |
| コンパイル時間 | 1,242 ms |
| コンパイル使用メモリ | 161,404 KB |
| 実行使用メモリ | 13,756 KB |
| 最終ジャッジ日時 | 2024-07-16 03:16:05 |
| 合計ジャッジ時間 | 4,216 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 TLE * 1 -- * 19 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:70:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
70 | scanf("%lld", &n);
| ~~~~~^~~~~~~~~~~~
ソースコード
#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;
}
Kmcode1