public static void Main(string[] args) { int N = ri; int K = ri; int[] A = ria; Mint ans = 0; Sort(A); if (K == 1) { Console.WriteLine(0); return; } Mint[] maxCounts = new Mint[N]; Mint[] minCounts = new Mint[N]; for (int i = 0; i < N; i++) { // A[i]が最大値になる回数 maxCounts[i] = Combination.nCk(i, K - 1); // A[i]が最小値になる回数 minCounts[i] = Combination.nCk(N - i - 1, K - 1); } for (int i = 0; i < N; i++) { ans += A[i] * maxCounts[i]; ans -= A[i] * minCounts[i]; } Console.WriteLine(ans); }
public static Mint nCkForBigNSmallK(long n, long k) { Mint x = 1; Mint y = 1; for (int i = 0; i < k; i++) { x *= n - i; y *= i + 1; } return(x / y); }
} } public class Combination { static long SIZE = (long)1e7 + 1; static Mint[] mFactorial, mFactrialInverse, mInverse; private static void Init() { mFactorial = new Mint[SIZE]; mFactrialInverse = new Mint[SIZE]; mInverse = new Mint[SIZE]; mFactorial[0] = mFactorial[1] = 1; mFactrialInverse[0] = mFactrialInverse[1] = 1; mInverse[1] = 1; for (int i = 2; i < SIZE; i++) { mFactorial[i] = mFactorial[i - 1] * i; mInverse[i] = -1 * mInverse[Mint.MOD % i] * (Mint.MOD / i); mFactrialInverse[i] = mFactrialInverse[i - 1] * mInverse[i]; } }