結果

問題 No.660 家を通り過ぎないランダムウォーク問題
ユーザー HaarHaar
提出日時 2020-04-17 19:34:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 5,228 bytes
コンパイル時間 2,463 ms
コンパイル使用メモリ 202,060 KB
実行使用メモリ 12,536 KB
最終ジャッジ日時 2024-04-14 12:45:23
合計ジャッジ時間 4,285 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
11,124 KB
testcase_01 AC 13 ms
11,180 KB
testcase_02 AC 11 ms
11,144 KB
testcase_03 AC 11 ms
11,008 KB
testcase_04 AC 11 ms
11,220 KB
testcase_05 AC 12 ms
11,160 KB
testcase_06 AC 12 ms
11,164 KB
testcase_07 AC 11 ms
11,072 KB
testcase_08 AC 12 ms
11,016 KB
testcase_09 AC 12 ms
11,056 KB
testcase_10 AC 12 ms
11,216 KB
testcase_11 AC 11 ms
11,216 KB
testcase_12 AC 12 ms
11,064 KB
testcase_13 AC 11 ms
10,996 KB
testcase_14 AC 11 ms
10,988 KB
testcase_15 AC 12 ms
11,004 KB
testcase_16 AC 11 ms
11,008 KB
testcase_17 AC 12 ms
11,076 KB
testcase_18 AC 11 ms
10,988 KB
testcase_19 AC 11 ms
11,016 KB
testcase_20 AC 12 ms
11,216 KB
testcase_21 AC 12 ms
11,228 KB
testcase_22 AC 11 ms
11,112 KB
testcase_23 AC 12 ms
11,064 KB
testcase_24 AC 11 ms
11,080 KB
testcase_25 AC 11 ms
11,072 KB
testcase_26 AC 11 ms
11,012 KB
testcase_27 AC 12 ms
11,076 KB
testcase_28 AC 13 ms
11,208 KB
testcase_29 AC 12 ms
11,012 KB
testcase_30 AC 11 ms
11,260 KB
testcase_31 AC 11 ms
11,116 KB
testcase_32 AC 11 ms
11,216 KB
testcase_33 AC 12 ms
11,120 KB
testcase_34 AC 12 ms
11,196 KB
testcase_35 AC 13 ms
11,244 KB
testcase_36 AC 13 ms
11,344 KB
testcase_37 AC 13 ms
11,268 KB
testcase_38 AC 15 ms
11,716 KB
testcase_39 AC 16 ms
11,796 KB
testcase_40 AC 17 ms
11,708 KB
testcase_41 AC 19 ms
12,204 KB
testcase_42 AC 21 ms
12,304 KB
testcase_43 AC 21 ms
12,500 KB
testcase_44 AC 21 ms
12,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


template <uint32_t M> class ModInt{
public:
  uint64_t val;
  
  constexpr ModInt(): val(0){}
  constexpr ModInt(std::int64_t n){
    if(n >= M) val = n % M;
    else if(n < 0) val = n % M + M;
    else val = n;
  }
  
  inline constexpr auto operator+(const ModInt &a) const {return ModInt(val + a.val);}
  inline constexpr auto operator-(const ModInt &a) const {return ModInt(val - a.val);}
  inline constexpr auto operator*(const ModInt &a) const {return ModInt(val * a.val);}
  inline constexpr auto operator/(const ModInt &a) const {return ModInt(val * a.inv().val);}
  
  inline constexpr auto& operator=(const ModInt &a){val = a.val; return *this;}
  inline constexpr auto& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;}
  inline constexpr auto& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;}
  inline constexpr auto& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;}
  inline constexpr auto& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;}
 
  inline constexpr bool operator==(const ModInt &a) const {return val == a.val;}
  inline constexpr bool operator!=(const ModInt &a) const {return val != a.val;}
 
  inline constexpr auto& operator++(){*this += 1; return *this;}
  inline constexpr auto& operator--(){*this -= 1; return *this;}
 
  inline constexpr auto operator++(int){auto t = *this; *this += 1; return t;}
  inline constexpr auto operator--(int){auto t = *this; *this -= 1; return t;}
 
  inline constexpr static ModInt power(int64_t n, int64_t p){
    if(p < 0) return power(n, -p).inv();
    
    int64_t ret = 1, e = n;
    for(; p; (e *= e) %= M, p >>= 1) if(p & 1) (ret *= e) %= M;
    return ret;
  }
 
  inline constexpr static ModInt inv(int64_t a){
    int64_t b = M, u = 1, v = 0;
    
    while(b){
      int64_t t = a / b;
      a -= t * b; std::swap(a,b);
      u -= t * v; std::swap(u,v);
    }
 
    u %= M;
    if(u < 0) u += M;
    
    return u;
  }
 
  inline constexpr static auto frac(int64_t a, int64_t b){return ModInt(a) / ModInt(b);}
  
  inline constexpr auto power(int64_t p) const {return power(val, p);}
  inline constexpr auto inv() const {return inv(val);}
 
  friend inline constexpr auto operator-(const ModInt &a){return ModInt(-a.val);}
 
  friend inline constexpr auto operator+(int64_t a, const ModInt &b){return ModInt(a) + b;}
  friend inline constexpr auto operator-(int64_t a, const ModInt &b){return ModInt(a) - b;}
  friend inline constexpr auto operator*(int64_t a, const ModInt &b){return ModInt(a) * b;}
  friend inline constexpr auto operator/(int64_t a, const ModInt &b){return ModInt(a) / b;}
 
  friend std::istream& operator>>(std::istream &s, ModInt<M> &a){s >> a.val; return s;}
  friend std::ostream& operator<<(std::ostream &s, const ModInt<M> &a){s << a.val; return s;}

  template <int N>
  inline static auto div(){
    static auto value = inv(N);
    return value;
  }
};


/**
 * @attention 使用前にinit関数を呼び出す
 */
template <typename T> class Combinatorics{
public:
  static std::vector<T> facto;
  static std::vector<T> ifacto;

  static void init(int N){
    facto.assign(N+1, 1);
    ifacto.assign(N+1, 1);

    for(int i = 1; i <= N; ++i){
      facto[i] = facto[i-1] * i;
    }

    ifacto[N] = facto[N].inv();

    for(int i = N-1; i >= 0; --i){
      ifacto[i] = ifacto[i+1] * (i+1);
    }
  }

  static T f(int64_t i){
    assert(i < facto.size());
    return facto[i];
  }
  
  static T finv(int64_t i){
    assert(i < ifacto.size());
    return ifacto[i];
  }

  static T P(int64_t n, int64_t k);
  static T C(int64_t n, int64_t k);
  static T H(int64_t n, int64_t k);
  static T stirling_number(int64_t n, int64_t k);
  static T bell_number(int64_t n, int64_t k);
  static std::vector<T> bernoulli_number(int64_t n);
  static T catalan_number(int64_t n);
};

template <typename T> std::vector<T> Combinatorics<T>::facto = std::vector<T>();
template <typename T> std::vector<T> Combinatorics<T>::ifacto = std::vector<T>();

template <typename T> T Combinatorics<T>::P(int64_t n, int64_t k){
  if(n < k or n < 0 or k < 0) return 0;
  return f(n) * finv(n-k);
}

template <typename T> T Combinatorics<T>::C(int64_t n, int64_t k){
  if(n < k or n < 0 or k < 0) return 0;
  return P(n,k) * finv(k);
}

template <typename T> T Combinatorics<T>::H(int64_t n, int64_t k){
  if(n == 0 and k == 0) return 1;
  return C(n+k-1, k);
}


/**
 * @brief c_0 = 1, c_{n+1} = ∑_{i=0}^n c_i * c_{n-i} を満たす数列の第n項。
 * @note 長さ2nの対応の取れた括弧列の総数はc_n通り。
 */
template <typename T> T Combinatorics<T>::catalan_number(int64_t n){
  return C(2*n,n) - C(2*n,n-1);
}


using mint = ModInt<1000000007>;
using C = Combinatorics<mint>;

int main(){
  C::init(500000);
  
  int N; std::cin >> N;

  std::vector<mint> c(N+1);
  for(int i = 0; i <= N; ++i){
    c[i] = C::catalan_number(i);
  }

  for(int i = 1; i <= N; ++i){
    c[i] += c[i-1];
  }


  mint ans = 0;
  
  for(int k = 0; k <= N / 2; ++k){
    ans += C::C(N+2*k, k);
  }

  for(int k = 0; k < N / 2; ++k){
    ans -= C::C(N+2*k, k) * c[N/2-k-1] * 2;
  }

  std::cout << ans << std::endl;

  return 0;
}
0