267 字
1 分钟
序列
输入
5 3
2 4 2 3 4
输出
39
题解
从比他小的中找
组合数一搞就行
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const LL P = 1e9 + 7;
LL F[100005];
void Init()
{
F[0] = 1;
for (int i = 1; i <= 100000; i++)
F[i] = i * F[i - 1] % P;
}
LL pow_mod(LL a, int b)
{
LL ans = 1;
while (b)
{
if (b & 1)
ans = ans * a % P;
b >>= 1;
a = a * a % P;
}
return ans;
}
LL C(int n, int m)
{
if (m > n || m < 0)
return 0;
return F[n] * pow_mod(F[m] * F[n - m] % P, P - 2) % P;
}
LL a[100005], Has[100005];
int Sum[100005], n;
#define lowbit(_) ((_) & (-_))
void add(int x, int c)
{
for (int i = x; i <= n; i += lowbit(i))
Sum[i] += c;
}
int Query(int x)
{
int ans = 0;
for (int i = x; i > 0; i -= lowbit(i))
ans += Sum[i];
return ans;
}
int fron[100005], nxt[100005];
int main()
{
Init();
int K;
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
}
sort(a + 1, a + n + 1);
LL ans = 0;
for (int i = 1; i <= n; i++)
{
ans = (ans + a[i] * C(i - 1, K - 1)) % P;
}
printf("%lld\n", ans);
}