結果

問題 No.502 階乗を計算するだけ
ユーザー 小指が強い人小指が強い人
提出日時 2017-04-09 02:19:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,498 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 36,320 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-24 23:43:17
合計ジャッジ時間 16,911 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")

#include <cstdio>

inline int fact_mod(int n, int mod) {
  using ll = long long;
  using ull = unsigned long long;

  unsigned inv = 1;
  for (int i = 0; i < 5; ++i) inv *= 2 - inv * unsigned(mod);
  int r2 = -ull(mod) % mod; 

  // 仮定: x < mod * 2^32
  auto reduce = [&] (ull x) {
    ull y = ull(unsigned(x) * inv) * mod;
    return int(x >> 32) + mod - int(y >> 32); // (0, 2 * mod)

    // [0, mod) に制限する場合は、上の行を以下のように変更する。
    // int ret = int(x >> 32) - int(y >> 32);
    // return ret < 0 ? ret + mod : ret;
  };
  auto init = [&] (int n) {
    return reduce(ll(n) * r2);
  };
  auto add_mod = [&] (int a, int b) {
    return (a += b - mod) < 0 ? a + mod : a;
  };

  const int M = 24;
  const int diff = init(M);
  int fact[M], mult[M];
  for (int i = 0; i < M; ++i) fact[i] = init(1);
  for (int i = 0; i < M; ++i) mult[i] = init(i + 1);
  for (int i = 0; i < n / M; ++i) {
    for (int j = 0; j < M; ++j) fact[j] = reduce(ll(fact[j]) * mult[j]);
    for (int j = 0; j < M; ++j) mult[j] = add_mod(mult[j], diff);
  }
  int ret = init(1);
  for (int i = 0; i < M; ++i) {
    ret = reduce(ll(ret) * fact[i]);
  }
  ret = reduce(ret);
  for (int i = n / M * M + 1; i <= n; ++i) {
    ret = ll(ret) * i % mod;
  }
  return ret;
}

int main() {
  const int mod = 1e9 + 7;
  int n;
  scanf("%d",&n);
  if(n>=mod){printf("0\n");return 0;}
  printf("%d\n", fact_mod(n, mod));
  return 0;
}
0