Submission #12009858


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }

typedef pair<int, int> P;
ll modnum = 1e9+7;

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

const int MAX = 1e5+5;

long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % modnum;
        inv[i] = modnum - inv[modnum%i] * (modnum / i) % modnum;
        finv[i] = finv[i - 1] * inv[i] % modnum;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % modnum) % modnum;
}

int main() {
    ll n, k; cin >> n >> k;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    // 二項係数テーブルを作成しておく
    COMinit();

    sort(a.begin(), a.end());
    ll ans = 0;
    rep(i, n) {
        if(i >= k-1) {
            ans += COM(i, k-1) * a[i];
            ans %= modnum;
        }
        if(n-i-1 >= k-1) {
            ans -= COM(n-i-1, k-1) * a[i];
            ans %= modnum;
        }
    }

    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task E - Max-Min Sums
User esaka
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1800 Byte
Status AC
Exec Time 58 ms
Memory 3328 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 18
Set Name Test Cases
Sample sample_01, sample_02, sample_03, sample_04
All max_01, max_02, max_03, max_04, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, sample_01, sample_02, sample_03, sample_04
Case Name Status Exec Time Memory
max_01 AC 58 ms 3328 KiB
max_02 AC 57 ms 3328 KiB
max_03 AC 57 ms 3328 KiB
max_04 AC 58 ms 3328 KiB
random_01 AC 39 ms 3072 KiB
random_02 AC 11 ms 2688 KiB
random_03 AC 56 ms 3328 KiB
random_04 AC 53 ms 3328 KiB
random_05 AC 40 ms 3072 KiB
random_06 AC 12 ms 2688 KiB
random_07 AC 12 ms 2688 KiB
random_08 AC 9 ms 2560 KiB
random_09 AC 32 ms 2944 KiB
random_10 AC 18 ms 2816 KiB
sample_01 AC 8 ms 2560 KiB
sample_02 AC 8 ms 2560 KiB
sample_03 AC 8 ms 2560 KiB
sample_04 AC 8 ms 2560 KiB