Skip to content

Commit d43e153

Browse files
committed
Add problem from URI #math #combinatoric
1 parent cc4facc commit d43e153

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

URI/1904.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// https://www.urionlinejudge.com.br/judge/en/problems/view/1904
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
#define endl '\n'
6+
#define D(x) cout << #x << " = " << (x) << endl;
7+
8+
const int mx = 1e7 + 10;
9+
long long fact[mx];
10+
11+
vector<long long> primes;
12+
long long cnt_primes[mx];
13+
vector<long long> criba(mx, true);
14+
15+
// test parity for C(n, k)
16+
bool calc (long long n, long long k) {
17+
return (k & (n - k)) ? 0 : 1;
18+
}
19+
20+
long long count_primes (long long a, long long b) {
21+
return cnt_primes[b] - cnt_primes[a - 1];
22+
}
23+
24+
void gen_primes () {
25+
for (int i = 2; i < mx; ++i) {
26+
if (criba[i]) {
27+
int j = 2;
28+
while (i * j < mx) {
29+
criba[i * j] = false;
30+
j ++;
31+
}
32+
}
33+
}
34+
35+
criba[0] = criba[1] = 0;
36+
}
37+
38+
int main() {
39+
gen_primes();
40+
long long a, b;
41+
42+
for (int i = 1; i < mx; ++i) {
43+
criba[i] += criba[i - 1];
44+
}
45+
46+
while (cin >> a >> b) {
47+
if (a > b) swap(a, b);
48+
49+
if (a == b) {
50+
cout << "?" << endl;
51+
continue;
52+
}
53+
54+
55+
long long n = b - a;
56+
long long r = criba[b] - criba[max(0LL, a - 1)];
57+
58+
// |n+r-1| (n+r-1)! |n+r-1| n!
59+
// | | = ---------- = | | = ----------
60+
// | r | r! * (n-1)! | n | r! * (n-r)!
61+
//
62+
// from stars and bars Theorem
63+
64+
bool ans = calc(n + r - 1, n);
65+
66+
cout << (ans ? "Alice" : "Bob") << endl;
67+
}
68+
return 0;
69+
}

0 commit comments

Comments
 (0)