Submission #1196774


Source Code Expand

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
static const int MOD = 1000000007;

template <typename T>
static std::pair<T, T> extended_gcd(T a, T b){
	if(b == 0){ return std::pair<T, T>(1, 0); }
	const auto p = extended_gcd(b, a % b);
	return std::pair<T, T>(p.second, p.first - a / b * p.second);
}

template <int MOD>
class modulus_integer {

public:
	typedef modulus_integer<MOD> self_type;

private:
	int m_value;

	static self_type unsafe_construct(int x) noexcept {
		self_type y;
		y.m_value = x;
		return y;
	}

public:
	modulus_integer() noexcept
		: m_value(0)
	{ }

	modulus_integer(int x) noexcept
		: m_value(x % MOD)
	{
		if(m_value < 0){ m_value += MOD; }
	}


	int operator*() const noexcept { return m_value; }


	self_type& operator=(const self_type& x) noexcept {
		m_value = x.m_value;
		return *this;
	}


	bool operator==(const self_type& x) const noexcept {
		return m_value == x.m_value;
	}

	bool operator!=(const self_type& x) const noexcept {
		return m_value != x.m_value;
	}

	bool operator<(const self_type& x) const noexcept {
		return m_value < x.m_value;
	}

	bool operator<=(const self_type& x) const noexcept {
		return m_value <= x.m_value;
	}

	bool operator>(const self_type& x) const noexcept {
		return m_value > x.m_value;
	}

	bool operator>=(const self_type& x) const noexcept {
		return m_value >= x.m_value;
	}


	self_type operator+() const noexcept {
		return *this;
	}

	self_type operator-() const noexcept {
		return unsafe_construct(m_value > 0 ? MOD - m_value : 0);
	}


	self_type operator+(const self_type& x) const noexcept {
		const int y = m_value + x.m_value;
		return unsafe_construct(y >= MOD ? y - MOD : y);
	}

	self_type operator-(const self_type& x) const noexcept {
		const int y = m_value - x.m_value;
		return unsafe_construct(y < 0 ? y + MOD : y);
	}

	self_type operator*(const self_type& x) const noexcept {
		return unsafe_construct(
			static_cast<long long>(m_value) * x.m_value % MOD);
	}

	self_type operator/(const self_type& x) const {
		return (*this) * self_type(extended_gcd(x.m_value, MOD).first);
	}


	self_type& operator+=(const self_type& x) noexcept {
		return (*this = *this + x);
	}

	self_type& operator-=(const self_type &x) noexcept {
		return (*this = *this - x);
	}

	self_type& operator*=(const self_type& x) noexcept {
		return (*this = *this * x);
	}

	self_type& operator/=(const self_type& x){
		return (*this = *this / x);
	}


	self_type& operator++() noexcept {
		if(++m_value >= MOD){ m_value = 0; }
		return *this;
	}

	self_type& operator--() noexcept {
		if(--m_value < 0){ m_value = MOD - 1; }
		return *this;
	}

	self_type operator++(int) noexcept {
		self_type t = *this;
		if(++m_value >= MOD){ m_value = 0; }
		return t;
	}

	self_type operator--(int) noexcept {
		self_type t = *this;
		if(--m_value < 0){ m_value = MOD - 1; }
		return t;
	}

};

using mint = modulus_integer<MOD>;

inline mint modulus_factorial(int n){
	static std::vector<mint> table(1, 1);
	while(static_cast<int>(table.size()) <= n){
		const mint x(static_cast<int>(table.size()));
		table.push_back(x * table.back());
	}
	return table[n];
}

inline mint modulus_inv_factorial(int n){
	static std::vector<mint> table(1, 1);
	while(static_cast<int>(table.size()) <= n){
		const mint x(static_cast<int>(table.size()));
		table.push_back(table.back() / x);
	}
	return table[n];
}

inline mint modulus_combination(int n, int k){
	if(k < 0 || n < k){ return 0; }
	const mint a = modulus_factorial(n);
	const mint b = modulus_inv_factorial(n - k);
	const mint c = modulus_inv_factorial(k);
	return a * b * c;
}

int main(){
	ios_base::sync_with_stdio(false);
	int n, x, y;
	cin >> n >> x >> y;
	vector<ll> fact(n + 1);
	fact[0] = 1ll;
	for(int i = 1; i <= n; ++i){
		fact[i] = (fact[i - 1] * i) % MOD;
	}
	vector<vector<int>> raw_balls(n);
	for(int i = 0; i < n; ++i){
		int c, w;
		cin >> c >> w;
		--c;
		raw_balls[c].push_back(w);
	}
	vector<vector<int>> balls;
	for(auto& b : raw_balls){
		if(b.empty()){ continue; }
		sort(b.begin(), b.end());
		balls.emplace_back(move(b));
	}
	const int m = balls.size();
	sort(
		balls.begin(), balls.end(),
		[](const vector<int>& a, const vector<int>& b){ return a[0] < b[0]; });
	int outer_l = 0, outer_r = m;
	while(outer_l < outer_r){
		const int c = outer_l + (outer_r - outer_l) / 2;
		if(balls[0][0] + balls[c][0] <= y){
			outer_l = c + 1;
		}else{
			outer_r = c;
		}
	}
	int outer_count = 0;
	mint answer = 1;
	for(int i = 0; i < outer_l; ++i){
		int limit = x - balls[i][0];
		if(i == 0){
			if(outer_l >= 2){ limit = max(limit, y - balls[1][0]); }
		}else{
			limit = max(limit, y - balls[0][0]);
		}
		int l = 0, r = balls[i].size();
		while(l < r){
			const int c = l + (r - l) / 2;
			if(balls[i][c] <= limit){
				l = c + 1;
			}else{
				r = c;
			}
		}
		answer *= modulus_combination(outer_count + l, l);
		outer_count += l;
	}
	cout << *answer << endl;
	return 0;
}

Submission Info

Submission Time
Task D - Colorful Balls
User logicmachine
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 5246 Byte
Status AC
Exec Time 122 ms
Memory 19184 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 3
AC × 57
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt
All 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
01.txt AC 1 ms 256 KB
02.txt AC 1 ms 256 KB
03.txt AC 4 ms 768 KB
04.txt AC 1 ms 256 KB
05.txt AC 2 ms 512 KB
06.txt AC 1 ms 256 KB
07.txt AC 43 ms 6388 KB
08.txt AC 2 ms 384 KB
09.txt AC 22 ms 3196 KB
10.txt AC 15 ms 2112 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
13.txt AC 5 ms 896 KB
14.txt AC 1 ms 256 KB
15.txt AC 22 ms 3576 KB
16.txt AC 38 ms 6264 KB
17.txt AC 7 ms 1152 KB
18.txt AC 16 ms 2560 KB
19.txt AC 32 ms 4668 KB
20.txt AC 103 ms 14704 KB
21.txt AC 101 ms 14704 KB
22.txt AC 92 ms 14452 KB
23.txt AC 112 ms 16112 KB
24.txt AC 91 ms 14836 KB
25.txt AC 103 ms 14964 KB
26.txt AC 118 ms 16112 KB
27.txt AC 84 ms 15092 KB
28.txt AC 122 ms 16240 KB
29.txt AC 111 ms 15984 KB
30.txt AC 90 ms 13684 KB
31.txt AC 116 ms 16240 KB
32.txt AC 95 ms 14192 KB
33.txt AC 115 ms 16112 KB
34.txt AC 103 ms 14960 KB
35.txt AC 70 ms 9588 KB
36.txt AC 80 ms 10356 KB
37.txt AC 71 ms 9588 KB
38.txt AC 70 ms 9328 KB
39.txt AC 71 ms 9584 KB
40.txt AC 107 ms 16112 KB
41.txt AC 104 ms 17776 KB
42.txt AC 100 ms 18288 KB
43.txt AC 93 ms 18544 KB
44.txt AC 114 ms 17648 KB
45.txt AC 92 ms 19184 KB
46.txt AC 104 ms 18672 KB
47.txt AC 70 ms 9332 KB
48.txt AC 62 ms 8436 KB
49.txt AC 68 ms 8692 KB
50.txt AC 82 ms 14700 KB
51.txt AC 77 ms 13676 KB
52.txt AC 61 ms 8712 KB
53.txt AC 61 ms 8712 KB
54.txt AC 61 ms 8712 KB