結果

問題 No.1102 Remnants
ユーザー LanatusLanatus
提出日時 2024-01-06 12:09:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 790 ms / 2,000 ms
コード長 1,354 bytes
コンパイル時間 1,434 ms
コンパイル使用メモリ 112,228 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-01-06 12:09:27
合計ジャッジ時間 10,734 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 552 ms
6,676 KB
testcase_03 AC 521 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 169 ms
16,384 KB
testcase_06 AC 30 ms
6,676 KB
testcase_07 AC 790 ms
18,944 KB
testcase_08 AC 4 ms
6,676 KB
testcase_09 AC 10 ms
6,676 KB
testcase_10 AC 353 ms
6,676 KB
testcase_11 AC 157 ms
6,676 KB
testcase_12 AC 261 ms
6,676 KB
testcase_13 AC 10 ms
6,676 KB
testcase_14 AC 96 ms
10,496 KB
testcase_15 AC 73 ms
8,832 KB
testcase_16 AC 202 ms
17,664 KB
testcase_17 AC 190 ms
16,768 KB
testcase_18 AC 234 ms
16,896 KB
testcase_19 AC 67 ms
7,936 KB
testcase_20 AC 199 ms
6,676 KB
testcase_21 AC 561 ms
11,392 KB
testcase_22 AC 702 ms
14,848 KB
testcase_23 AC 546 ms
12,160 KB
testcase_24 AC 393 ms
9,728 KB
testcase_25 AC 450 ms
17,152 KB
testcase_26 AC 36 ms
6,676 KB
testcase_27 AC 449 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <vector>
#include <tuple>
#include <set>
#include <map>

using namespace std;

using ll = long long;
const ll INFLL = 1e18;
const ll MOD = 1e9 + 7;

struct Comb {
  ll n, k;
  map<ll,ll> cache;

  Comb(ll _n, ll _k): n(_n), k(_k) {
    build();
  }

  void build() {
    ll b = 1;
    ll p = 1;

    for (ll i = 0; i < k; ++i) {
      b *= (n + k - 1 - i);
      b %= MOD;

      p *= i + 1;
      p %= MOD;
    }

    b = (b * modpow(p, MOD - 2)) % MOD;
    cache[n + k - 1] = b;

    for (ll i = n - 1; i >= 1; --i) {
      b = (b * i) % MOD * modpow(i + k, MOD - 2) % MOD;
      cache[i + k - 1] = b;
    }
  }

  ll get(ll n, ll x) {
    // cout << n - x + k << " " << x + k - 1 << endl;
    return (cache[n - x + k] * cache[x + k - 1]) % MOD;
  }

  ll modpow(ll a, ll n) {
    if (n == 0) return 1;
    if (n % 2) return a * modpow(a, n - 1) % MOD;
    ll t = modpow(a, n / 2);
    return (t * t) % MOD;
  }

};

int main() {
  ll n, k;
  cin >> n >> k;

  Comb com(n, k);
  vector<ll> T(n + 1);

  for (int i = 1; i <= n; ++i) {
    T[i] = com.get(n, i);
  }

  vector<ll> a(n + 1);
  for (int i = 1; i <= n; ++i) cin >> a[i];

  ll sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (a[i] * T[i]) % MOD;
    sum %= MOD;
  }

  cout << sum << endl;
  return 0;
}
0