結果

問題 No.797 Noelちゃんとピラミッド
ユーザー tsugittatsugitta
提出日時 2020-03-31 00:49:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 4,762 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 98,308 KB
実行使用メモリ 8,712 KB
最終ジャッジ日時 2023-09-05 12:59:12
合計ジャッジ時間 5,034 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 21 ms
8,668 KB
testcase_04 AC 22 ms
8,524 KB
testcase_05 AC 22 ms
8,524 KB
testcase_06 AC 21 ms
8,572 KB
testcase_07 AC 22 ms
8,520 KB
testcase_08 AC 22 ms
8,512 KB
testcase_09 AC 22 ms
8,644 KB
testcase_10 AC 21 ms
8,664 KB
testcase_11 AC 21 ms
8,552 KB
testcase_12 AC 21 ms
8,528 KB
testcase_13 AC 21 ms
8,604 KB
testcase_14 AC 22 ms
8,712 KB
testcase_15 AC 21 ms
8,512 KB
testcase_16 AC 21 ms
8,576 KB
testcase_17 AC 22 ms
8,532 KB
testcase_18 AC 21 ms
8,668 KB
testcase_19 AC 21 ms
8,576 KB
testcase_20 AC 21 ms
8,672 KB
testcase_21 AC 21 ms
8,648 KB
testcase_22 AC 22 ms
8,528 KB
testcase_23 AC 15 ms
7,064 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 14 ms
6,520 KB
testcase_26 AC 13 ms
6,252 KB
testcase_27 AC 21 ms
8,324 KB
testcase_28 AC 19 ms
7,820 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 5 ms
4,376 KB
testcase_31 AC 16 ms
7,084 KB
testcase_32 AC 8 ms
4,624 KB
testcase_33 AC 13 ms
6,288 KB
testcase_34 AC 9 ms
5,440 KB
testcase_35 AC 21 ms
8,632 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 18 ms
7,716 KB
testcase_38 AC 19 ms
7,976 KB
testcase_39 AC 13 ms
6,132 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 5 ms
4,380 KB
testcase_42 AC 13 ms
6,260 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 1 ms
4,380 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://atcoder.jp/contests/arc081/tasks/arc081_b

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<ll> v);
template <typename T>
void drop(T a);

const ll INF = 1e18;

template <int MOD>
class Fp {
 public:
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0) val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp& r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp& r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp& r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp& r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp& operator+=(const Fp& r) noexcept {
    val += r.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  constexpr Fp& operator-=(const Fp& r) noexcept {
    val -= r.val;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr Fp& operator*=(const Fp& r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp& operator/=(const Fp& r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0) val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp& r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp& r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream& operator<<(ostream& os, const Fp<MOD>& x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD>& a, long long n) noexcept {
    if (n == 0) return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1) t = t * a;
    return t;
  }
};

// const ll MOD = 998244353;
const ll MOD = 1000000007;
using mint = Fp<MOD>;

struct combination_table {
  // i! % MOD M
  V<ll> fact;
  // (i!)^(-1) MOD M
  V<ll> finv;
  // i^(-1) MOD M
  V<ll> inv;
};

combination_table ct;

// ref: http://drken1215.hatenablog.com/entry/2018/06/08/210000
void prepare_combination(ll max) {
  V<ll> fact(max + 1);
  V<ll> finv(max + 1);
  V<ll> inv(max + 1);

  fact[0] = 1;
  fact[1] = 1;
  finv[0] = 1;
  finv[1] = 1;
  inv[1] = 1;

  for (ll i = 2; i <= max; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) %
                       MOD;  // a^(-1) ≡ -(p % a)^(-1) * (p/a) MOD p
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }

  ct = {
      fact,
      finv,
      inv,
  };
}

mint c_m(ll n, ll k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return mint(ct.fact[n]) * ct.finv[k] * ct.finv[n - k];
}

// 重複組合せ n 種類から k 個重複を許して取る通り
mint h_m(ll n, ll k) { return c_m((n - 1) + k, k); }

mint p_m(ll n, ll k, combination_table& ct) { return c_m(n, k) * ct.fact[k]; }

void solve() {
  ll N;
  cin >> N;

  VL as(N);
  rep(i, N) cin >> as[i];
  prepare_combination(N - 1);

  mint sum = 0;

  rep(i, N) { sum += c_m(N - 1, i) * as[i]; }

  cout << sum << "\n";
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

#ifndef TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  try {
    solve();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void print_ints(vector<ll> v) {
  rep(i, v.size()) {
    if (i > 0) {
      cout << " ";
    }

    cout << v[i];
  }

  cout << endl;
}

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}
0