結果

問題 No.1102 Remnants
ユーザー fastmathfastmath
提出日時 2020-07-03 22:02:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 982 ms / 2,000 ms
コード長 1,867 bytes
コンパイル時間 1,721 ms
コンパイル使用メモリ 171,044 KB
実行使用メモリ 4,908 KB
最終ジャッジ日時 2023-10-17 02:29:43
合計ジャッジ時間 12,573 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 903 ms
4,348 KB
testcase_03 AC 872 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 15 ms
4,908 KB
testcase_06 AC 12 ms
4,348 KB
testcase_07 AC 982 ms
4,764 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 590 ms
4,348 KB
testcase_11 AC 253 ms
4,348 KB
testcase_12 AC 424 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 29 ms
4,348 KB
testcase_16 AC 72 ms
4,764 KB
testcase_17 AC 69 ms
4,500 KB
testcase_18 AC 72 ms
4,500 KB
testcase_19 AC 25 ms
4,348 KB
testcase_20 AC 311 ms
4,348 KB
testcase_21 AC 774 ms
4,348 KB
testcase_22 AC 899 ms
4,348 KB
testcase_23 AC 733 ms
4,348 KB
testcase_24 AC 533 ms
4,348 KB
testcase_25 AC 457 ms
4,500 KB
testcase_26 AC 48 ms
4,348 KB
testcase_27 AC 683 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';

const int MOD = 1000 * 1000 * 1000 + 7;

int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }

int C(int n, int k) {
    int ans = 1;
    for (int i = 1; i <= n; ++i)
        ans = mod(ans * i);
    int d = 1;
    for (int i = 1; i <= n - k; ++i)
        d = mod(d * i);
    for (int i = 1; i <= k; ++i)
        d = mod(d * i);
    return dv(ans, d);
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int n, k;
    cin >> n >> k;
    vector <int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];

    if (k == 0) {
        int sum = 0;
        for (int i = 0; i < n; ++i)
            sum = mod(sum + a[i]);
        cout << sum << endl;
        exit(0);
    }   

    int ans = 0;

    int lf = fp(k, MOD - 2);
    int rf = C(n + k, k);
    for (int i = 0; i < n; ++i) {
        int l = i + 1;
        int r = n - i;

        lf = mod(lf * (l + k - 1));
        if (l > 1) {
            lf = dv(lf, l - 1);
        }

        rf = dv(rf, r + k);
        rf = mod(rf * r);

        ans = mod(ans + mod(lf * rf) * a[i]);
        // C(l + k - 1, k) * C(r + k - 1, k)
    }   

    cout << ans << endl;
}
0