結果

問題 No.502 階乗を計算するだけ
ユーザー ynayna
提出日時 2019-07-23 20:23:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 4,664 bytes
コンパイル時間 1,661 ms
コンパイル使用メモリ 167,968 KB
実行使用メモリ 814,732 KB
最終ジャッジ日時 2023-09-08 15:20:23
合計ジャッジ時間 4,921 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 16 ms
10,336 KB
testcase_23 AC 6 ms
4,840 KB
testcase_24 AC 12 ms
7,920 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 7 ms
5,656 KB
testcase_27 AC 6 ms
4,764 KB
testcase_28 AC 7 ms
5,456 KB
testcase_29 AC 4 ms
4,384 KB
testcase_30 AC 15 ms
9,936 KB
testcase_31 AC 9 ms
6,212 KB
testcase_32 MLE -
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>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

#ifndef MOD_INT
#define MOD_INT
template<int mod>
struct ModInt{
  int x;

  ModInt() : x(0){}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){}

  ModInt &operator+=(const ModInt &p){
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p){
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p){
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p){
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const {return ModInt(-x);}
  ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}
  ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}
  ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}
  ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}
  bool operator==(const ModInt &p) const {return x == p.x;}
  bool operator!=(const ModInt &p) const {return x != p.x;}

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p){
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a){
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod(){return mod;}
};

using mint = ModInt<MOD>;
#endif
#ifndef MOD_INT
#define MOD_INT
template<int mod>
struct ModInt{
  int x;

  ModInt() : x(0){}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){}

  ModInt &operator+=(const ModInt &p){
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p){
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p){
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p){
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const {return ModInt(-x);}
  ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}
  ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}
  ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}
  ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}
  bool operator==(const ModInt &p) const {return x == p.x;}
  bool operator!=(const ModInt &p) const {return x != p.x;}

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p){
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a){
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod(){return mod;}
};

using mint = ModInt<MOD>;
#endif

class Factorial{
    private:
        int max;
        vector<mint> fact, ifact;

    public:
        Factorial(int N):max(N), fact(N+1), ifact(N+1){
            fact[0] = 1;
            for(int i = 1; i <= N; i++) fact[i] = fact[i-1] * i;
            ifact[N] = fact[N].inverse();
            for(int i = N; i >= 1; i--) ifact[i-1] = ifact[i] * i;
        }

        void resize(int n){
            if(n > max){
                fact.resize(n);
                ifact.resize(n);
                for(int i = max + 1; i <= n; i++) fact[i] = fact[i-1] * i;
                ifact[n] = fact[n].inverse();
                for(int i = n; i >= max + 1; i--) ifact[i-1] = ifact[i] * i;
                max = n;
            }
        }

        mint operator()(int n){
            if(n > max) this -> resize(n);

            return fact[n];
        }

        mint inverse(int n){
            if(n > max) this -> resize(n);

            return ifact[n];
        }
};

int main(void){
    ll n;
    cin >> n;

    mint res;
    if(n >= MOD){
        res = 0;
    }else{
        Factorial fact(n);
        res = fact(n);
    }

    cout << res << '\n';

    return 0;
}
0