Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2022 /round2 /balance_scale.cpp
wjomlex's picture
2022 Problems
f7ba5f2 verified
raw
history blame
4.23 kB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
using LL = long long;
class mint {
LL val;
mint& normalize(LL x) {
val = x % MOD;
if (val < 0) {
val += MOD;
}
return *this;
}
static LL inverse(LL a) {
LL u = 0, v = 1, m = MOD;
while (a != 0) {
LL t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
public:
mint(LL x = 0) { normalize(x); }
LL operator()() const { return val; }
template <typename U>
explicit operator U() const { return static_cast<U>(val); }
mint& operator=(LL x) { return normalize(x); }
mint& operator=(const mint& x) { val = x.val; return *this; }
mint& operator+=(const mint& x) { return normalize(val + x.val); }
mint& operator-=(const mint& x) { return normalize(val - x.val); }
mint& operator*=(const mint& x) { return normalize(val * x.val); }
mint& operator/=(const mint& x) { return *this *= mint(inverse(x.val)); }
mint& operator+=(LL x) { return *this += mint(x); }
mint& operator-=(LL x) { return *this -= mint(x); }
mint& operator*=(LL x) { return *this *= mint(x); }
mint& operator/=(LL x) { return *this /= mint(x); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
mint operator++(int) { mint z(*this); ++*this; return z; }
mint operator--(int) { mint z(*this); --*this; return z; }
friend mint operator+(mint x, const mint& y) { return x += y; }
friend mint operator*(mint x, const mint& y) { return x *= y; }
friend mint operator-(mint x, const mint& y) { return x -= y; }
friend mint operator/(mint x, const mint& y) { return x /= y; }
friend mint operator+(mint x, LL y) { return x += y; }
friend mint operator*(mint x, LL y) { return x *= y; }
friend mint operator-(mint x, LL y) { return x -= y; }
friend mint operator/(mint x, LL y) { return x /= y; }
friend mint operator+(LL x, mint y) { return y += x; }
friend mint operator*(LL x, mint y) { return y *= x; }
friend mint operator-(LL x, const mint& y) { mint z(x); return z -= y; }
friend mint operator/(LL x, const mint& y) { mint z(x); return z /= y; }
bool operator <(const mint& x) const { return val < x.val; }
bool operator==(const mint& x) const { return val == x.val; }
bool operator >(const mint& x) const { return val > x.val; }
bool operator!=(const mint& x) const { return val != x.val; }
bool operator<=(const mint& x) const { return val <= x.val; }
bool operator>=(const mint& x) const { return val >= x.val; }
bool operator <(LL x) const { return val < x; }
bool operator==(LL x) const { return val == x; }
bool operator >(LL x) const { return val > x; }
bool operator!=(LL x) const { return val != x; }
bool operator<=(LL x) const { return val <= x; }
bool operator>=(LL x) const { return val >= x; }
};
class factorial : vector<mint> {
void lazy_eval(int n) {
for (LL p = size(); n >= p; ++p) {
push_back(back() * p);
}
}
public:
factorial() { push_back(1); }
mint choose(LL n, LL k) {
if (n < 0 || k < 0 || n < k) {
return 0;
}
if (k == 0 || k == n) {
return 1;
}
if (n >= MOD && n <= k + MOD - 1) {
return 0;
}
lazy_eval(n);
return at(n) / (at(k) * at(n - k));
}
mint operator[](LL n) {
lazy_eval(n);
return at(n);
}
};
factorial F;
LL solve() {
int N, K, c0, w0;
cin >> N >> K >> c0 >> w0;
LL lighter = 0, equal = 0, heavier = 0;
for (int i = 1, c, w; i < N; i++) {
cin >> c >> w;
if (w < w0) {
lighter += c;
} else if (w == w0) {
equal += c;
} else {
heavier += c;
}
}
LL total = c0 + lighter + equal + heavier;
LL not_heavier = total - heavier;
K++;
mint ways_some_equal = F.choose(not_heavier, K) - F.choose(lighter, K);
mint prob_some_equal = ways_some_equal / F.choose(total, K);
return LL(prob_some_equal * c0 / (c0 + equal));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}