Last active
December 12, 2015 03:38
-
-
Save yasuharu519/4708163 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup Rount1 #1 CardGame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <iostream> | |
#include <vector> | |
#include <map> | |
using namespace std; | |
#define MODULO 1000000007 | |
long factorial(long k) | |
{ | |
if(k == 1 || k == 0) | |
{ | |
return 1; | |
}else{ | |
return (k * factorial(k - 1)) % MODULO; | |
} | |
} | |
long solve(vector<long> data, long N, long K) | |
{ | |
vector<long>::iterator it; | |
long result = 0; | |
long fact_k = factorial(K-1); | |
long fact_n = fact_k; | |
long fact_n_k = 1; | |
long count = 1; | |
for(it = data.begin() + K - 1; it != data.end(); ++it) | |
{ | |
result += (*it) * fact_n / fact_k / fact_n_k; result %= MODULO; | |
fact_n_k *= count; fact_n_k %= MODULO; | |
fact_n *= (count + K-1); fact_n %= MODULO; | |
++count; | |
} | |
return result; | |
} | |
int main() | |
{ | |
int T; | |
long N, K; | |
scanf("%d", &T); | |
long chunk; | |
vector<long> data; | |
vector<long>::iterator it; | |
for(int i = 0; i < T; ++i) | |
{ | |
scanf("%ld %ld", &N, &K); | |
for(long j = 0; j < N; ++j){ | |
cin >> chunk; | |
data.push_back(chunk); | |
} | |
sort(data.begin(), data.end()); | |
printf("Case #%d: %ld\n", i+1, solve(data, N, K)); | |
data.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment