Thanks for participating. We hope you enjoyed the contest!
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
cout << (n / 10) + (n % 10) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
def f(a, b):
if (a > b): return 1
if (a == b): return 0
if (a < b): return -1
for _ in range(int(input())):
a, b, c, d = map(int, input().split())
ans = 0
if f(a, c) + f(b, d) > 0:
ans += 1
if f(a, d) + f(b, c) > 0:
ans += 1
if f(b, c) + f(a, d) > 0:
ans += 1
if f(b, d) + f(a, c) > 0:
ans += 1
print(ans)
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
def solve():
n, s, m = map(int, input().split())
segs = [[0, 0], [m, m]] + [list(map(int, input().split())) for i in range(n)]
segs.sort()
for i in range(1, n + 2):
if segs[i][0] - segs[i - 1][1] >= s:
print('YES')
return
print('NO')
#sys.stdin = open('in', 'r')
for _ in range(int(input())):
solve()
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int test_cases; cin >> test_cases;
while(test_cases--) {
string s, t; cin >> s >> t;
int idx = 0;
for(int i = 0; i < (int)s.size(); ++i) {
if(s[i] == '?') {
if(idx < (int)t.size()) s[i] = t[idx++];
else s[i] = 'a';
} else if(s[i] == t[idx]) ++idx;
}
if(idx >= t.size()) cout << "YES\n" << s << "\n";
else cout << "NO\n";
}
}
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200'007;
const int MOD = 1'000'000'007;
int a[MAX], psum[MAX];
int f(int x) {
int cnt = 0;
while (x) {
x /= 3;
cnt++;
}
return cnt;
}
void solve() {
int l, r;
cin >> l >> r;
cout << psum[r] - psum[l - 1] + a[l] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
psum[0] = 0;
for (int i = 1; i < MAX - 1; i++) {
a[i] = f(i);
psum[i] = psum[i - 1] + a[i];
}
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
Idea: mesanu
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;
int64_t fact[N];
int64_t pw(int64_t a, int64_t b) {
int64_t r = 1;
while(b > 0) {
if(b & 1) r = (r * a) % mod;
b /= 2;
a = (a * a) % mod;
}
return r;
}
int64_t C(int64_t n, int64_t k) {
if(n < k) return 0LL;
return (fact[n] * pw((fact[n - k] * fact[k]) % mod, mod - 2)) % mod;
}
int main() {
int t; cin >> t;
fact[0] = 1;
for(int64_t i = 1; i < N; ++i) fact[i] = (fact[i - 1] * i) % mod;
while(t--) {
int n, k; cin >> n >> k;
vector<int> a(n);
int ones = 0;
for(int i = 0; i < n; ++i) {
cin >> a[i];
ones += a[i];
}
//at least k/2+1 ones
int64_t ans = 0;
for(int cnt_ones = k / 2 + 1; cnt_ones <= min(ones, k); ++cnt_ones) {
ans += C(ones, cnt_ones) * C(n - ones, k - cnt_ones) % mod;
ans %= mod;
}
cout << ans << "\n";
}
}
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200'007;
const int MOD = 1'000'000'007;
void solve() {
int l = 2, r = 1000;
while (l < r) {
int mid = l + (r - l) / 2;
cout << "? 1 " << mid << endl;
int resp; cin >> resp;
if (resp == mid) {
l = mid + 1;
}
else {
r = mid;
}
}
cout << "! " << l << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200'007;
const int MOD = 1'000'000'007;
void solve() {
int l = 1, r = 999;
while (r - l > 2) {
int a = (2 * l + r) / 3;
int b = (2 * r + l) / 3;
cout << "? " << a << ' ' << b << endl;
int resp; cin >> resp;
if (resp == (a + 1) * (b + 1)) {
r = a;
}
else if (resp == a * b) {
l = b;
}
else {
l = a; r = b;
}
}
if (r - l == 2) {
cout << "? 1 " << l + 1 << endl;
int resp; cin >> resp;
if (resp == l + 1) {l = l + 1;}
else {r = l + 1;}
}
cout << "! " << r << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}








you are so quickly!
so fast!
use english, plz.
ok!
I will kill myself over B. UPD: Found my error
me but i found it after one hour :(
I spent lot's of time considering 1:0 situations in problem B.
fast editorial
Clarification: SlavicG and I actually are best friends, he's just playing hard to get.
In problem E, my code was giving me expected/correct output in my local machine, but is giving wrong output during codeforce's testing 274941437
Could any one tell why is that?
probably rounding errors with log. safer not to use log when you dont have to
G1<<E or F
G1<F maybe, but E is definitely easier
I had the same intuition for F but I got stuck and I had no idea why my code was failing for the last provided test case. Values seem fine, but it keeps dumping to stack trace, isn't it just a O(n) loop with my factorials memoized?
I think you forgot to mod at factorial. And you should add mod inverse too :D
Got the Logic Couldn't make it up.
You have to use floor instead of ceil. The bigger problem here was many people getting WA on test 2 i.e. 1 243, this happened because of the definition of log function which uses natural log to calculate the log of any number. This causes errors due in precision and floating point arithmetic in some cases. So either the better way is to just do repeated floor division by 3, or by changing how log3 is implemented. I defined log3(n) = log2(n)/log2(3) which worked better because log2 works with binary which means better precision and less errors.
because r <= 2e5, you can do the array f[n] = ceil(log(n)), then f[i] = f[i/3] + 1, with f[0] = 0
In editorial of G2 , if a < x <= b, area should be a * (b + 1) but given (a+1) * b. May be it's a Typo SlavicG
Also, (2,a] should be [2, a]
Thanks, it'll be fixed soon.
I think G1 and G2 were much easier than E and F... the most basic binary search in both versions
they're equally easy... it's just in F you should be more careful when preprocessing the factorial and the inverse array, while in E, G1 and G2 it's much easier to implement the idea, which was already easy before
Why my code 274894324 is getting WA on E? I did all exactly as in editorial
check this testcase
1
243 244
l<r
Sorry...I have corrected it
try 1, 999
Just got an observation for E:
[1, 3) -> all elements require 1 operations to become 0
[3, 9) -> all elements require 2 operations to become 0
[9, 27) -> all elements require 3 operations to become 0
[27, 81) -> all elements require 4 operations to become 0
[81, 81 * 3) -> all elements require 5 operations to become 0
and so on.
I think this could be optimal.
Yes, this can reach $$$O(\log\log n)$$$ complexity and also feasible to $$$r\le 10^{18}$$$.
How to precompute in log(log(n)) complexity?
I mean O(loglogn) for each query
actually this is only needed in the preprocessing phase so it won't improve the code much. also, the complexity goes from
O(N + n)toO(log(log(N)) + n(log(n)), which is worse.I love E, F, G2. Brilliant idea!!!
Can anyone tell me how to test our code for an interactive problem?
You can write an answer function according to the description of problem. Then use it to simulate this whole process. Check my submission if you know python.
For problem E why is this O(n) code getting TLE?(https://codeforces.me/contest/1999/submission/274854161)
i dont think O(n) solutions passed, they used O(n) to precompute and then O(1) for each test case
Sum of $$$r-l$$$ is not bounded by $$$10^5$$$
For instance, $$$l=1, r=10^5$$$ can be given $$$10^4$$$ times. In that case, your approach will TLE
I don't understand. The time limit given in questions is 'per test case', then how do we analyze multiple test cases? Shouldn't O(n) for l = 1, r = 10^5 always pass in 1 second?
No, the time limit is per test file, not per test case. If $$$t = 10^4$$$, your code must solve all the $$$10^4$$$ test cases within the time limit. (If the code was allowed $$$1$$$ second to run per test case, it would take at most $$$10^4$$$ seconds to finish all the cases, which is obviously infeasible.)
The problem doesn't guarantee the sum of $$$r-l$$$ is below $$$2\times 10^5$$$
I'm so used to "Sum of n over all cases doesn't exceed $$$ 2 \times 10^5 $$$ " :)
In which test case my submission 274924034 for E is failing.
I dont understand why this gives wrong ans; E
Why is my D solution so slow? 274970418 I am using the same two pointer strategy that the editorial mentions and get TLE every time. I have a single loop iterating through the string and that is it. Am I using any expensive operations without realizing it? Thanks in advance.
In java, adding a letter to the end of a string is an O(n) operation, as it creates an entirely new string.
Can anyone tell that why in E O(n log n) doesn't work ? To be precise the overall complexity would be 2.5*10^6, which should pass in 1 sec. ? isn't it?
'cuz the complexity is actually $$$O(tn\log n)$$$. Usually the problem guarantees the sum of $$$n$$$, $$$m$$$, etc. doesn't exceed $$$2\times 10^5$$$, but this problem does not.
ahh i get that, thanks dude!
because sum of r-l over all test cases isnt guaranteed to be under 2*10^5, so your code runs in O(t*n*logn)
I got another simpler setup approach to E, perhaps.
We will make an array of the exponentiation of 3, like lt[0] = 1, lt[1] = 3, lt[2] = 9, lt[3] = 27, ..., lt[15] = 3^15. (Because r <= 2 . 10 ^ 5)
We will make a prefix sum before making the testcase. For each i, call j is the smallest number satisfying a[i] >= lt[j], (1 <= j <= 15).
For example, a[1] = 1, a[2] = 1, a[3] = 2, a[4] = 2, ..., a[8] = 2, a[9] = 3, a[10] = 3, ... After that we will make a prefix sum of a[] called f[]. For instance, f[1] = 1, f[2] = f[1] + a[2] = 2, f[3] = f[2] + a[3] = 4, ...
For each n, m in the testcase, to find the minimum operation needed, while n, m is typed from the keyboard, the answer will be pre[m] — pre[n-1].
Then plus the a[n], as we have to make n become 0 to minimize the operations, the final thing to do is f[m] — f[n-1] + a[n].
Link: My sub
this is actually the same as the editorial, it's just you're adding two pointer to it. but it's still better, as you're precomputing in
O(n). another similar idea is that you build the a[] array like this:a[i] = a[i/3] + 1for all1 <= i <= 2e5Can anyone help me find out the bug in my code for problem D? It passed all testcases, but it got hacked, and I don't know how to find the specific testcase in which it failed at.
https://codeforces.me/contest/1999/submission/274881476
Thanks in advance! If this is against the rules of the contest, please let me know so I can delete this comment.
Consider
aaaaaaaaaaaaaaaaaaaaabandaaaaaaaaaaac. Your code will compare $$$O(n^2)$$$ times in this case.Thanks!
You know for the Showering question how did you not get a memory exceeded error??
I am also using a linked list to store the timings which aren't available. But test case 3 threw a memory exceeded error.
The interval can be as large as $$$10^9$$$.
E is good, but isn't the data range a bit small? Many $$$O(Tn)$$$ algorithms can pass this problem, and it's hard to hack them.
You cannot have a $$$\mathcal{O}(Tn)$$$ algorithm for Problem E as the statement does not guarantee that the sum of all $$$n$$$ over all test cases does not exceed $$$2 \times 10^5$$$.
A $$$\mathcal{O}(Tn)$$$ algorithm could easily reach $$$2 \times 10^9$$$ iterations at worst, which is certainly not optimal to pass all tests under the time constraints imposed.
you are repeating what he said
For Problem E, I am running a loop here of size (b-a+1) each time. I tried hacking my solution on max size testcase $$$10000 \newline 1\;200000\newline....\newline....\newline1\;200000\newline$$$ . I don't know this solution got accepted? Can anybody tell the reason for the same or else try to hack it?
Submission link
the compiler isnt that stupid, it can optimize out obviously unnecessary parts
Firstly, you can't share hacks. Secondly, as the code works in $$$O(r - l)$$$ per test, the max amount of operations is 1e4 * 2e5, which is 2e9 operations, which is just enough to pass.
Why can't you share hacks?
2e9 operations shouldn't pass in 1 second. That part of the code is obviously optimized by the compiler.
The hacking phase is still running, Posting your hack is technically the same as sharing your solution during the contest.
You can't simply count the number of operations to decide whether it will run in a specific time or not. The 'weight' of an operation depends on many other aspects than just a simple number of CPU instructions, and in this case the naive part is a very very light one, because it's about simply iterating and summing through an array in order, which leads to perfect cache hit rates and doesn't include any kind of heavy operations. 2e9 of such operations can mostly fit in time with computers nowadays.
But now that I see it, that specific solution is just compiler-optimized, and it didn't actually run the loop 2e9 times.
Can I see where it is stated that open hacks can't be shared? It's a post-contest hack where people can discuss the solutions, so I don't see why the test part can't be discussed. Hacks don't even give additional points. I've never seen discussion on open hacks being prohibited by anyone before.
Sometimes, the hack tests are added to the main tests early during the phase and people can see them so they're not even guaranteed to be private.
Also, I think the main purpose of having an open hack phase is to strengthen the tests by making anti-tests against actual participants' solutions, because it is easy to miss them when you don't have enough samples to work with. For this purpose the discussion on hacks shouldn't be discouraged.
Noted! Learn from mistakes. I just was referring to
...
...
Harder Version of F if anyone wants to try. Sum of Medians
Can any one give me more explain for problem F , i did not Understand tut :(
For F, with x numbers 1, this formula $$$\binom{x}{k/2+1} * \binom{n-(k/2+1)}{k/2}$$$ is correct?
The logic is that we take $$$k/2+1$$$ from $$$x$$$ numbers of 1, and whatever others elements are(taking $$$k/2$$$ from the left $$$n - (k/2+1))$$$ the median will be 1
Can someone solve E when l and r are large i.e upto 1e18 it could possibly turn out to be a good math problem.Please do write if you have any idea on how to do it?
you can precompute the log(log3()) array, and then use binary search to find the answer
Notice that 3^n gets large really fast (3^38 > 1e18). For each x (3^i <= x < 3^(i+1)), it takes i operation to make x zero
And then i just need to multiply it i.e i*(x-3^i)?
Yes, there are 3^(i+1)-3^(i)-1 of such numbers for each i, handle ones with l,r carefully
https://codeforces.me/contest/1999/submission/274827875
I really appreciate problem E. I have seen the pattern: "To use the fewest number of operations possible, we should do the first step on the minimum number, since it has the fewest digits." after writing down a few cases, but I failed to come up with prefix sum part.
Thank you. Now I learn.
Can anyone let me know why this gives TLE.Even the solution code precomputes till 2e5.275010837
Because there are t sets of examples, and the total number is unlimited
u precomputed but then u still run from l to r
In problem E, function $$$f$$$ takes $$$O(\log_3 x)$$$ to compute on a number $$$x$$$, so your complexity is $$$O(n \log n)$$$, not $$$O(n)$$$. However, there is a simple way to compute $$$f(1), f(2), \ldots, f(n)$$$ in $$$O(n)$$$ overall, without utilizing any expensive math functions: notice that $$$f(x) = f(\lfloor x / 3 \rfloor) + 1$$$. The code is even shorter than before: 275014438.
G2 is just introduction to ternary search.
can anyone tell why my e code of o(n) is giving tle ? :- https://codeforces.me/contest/1999/submission/274942317
T×N complexity.
thnx :( . i hardly check size of T .would be remebered next time
I regret not studying interactive problems!
Can someone help which testcase I am missing
Well, the answer can only be 0, 2, 4. For any game, we have a selection of Round 1 and Round 2 for which we won, we can change the order to the rounds(Round 1 as the second round and vice versa). And ordering doesn't here doesn't change the winner.
When will we get the rating updates? Im kinda excited
Can somebody point out what is wrong?: 274922968
If problem F was to count odd length subsequences having median as 1. What can be the solution?
Alternate solution to problem E, works in O(log3(r))
Basically make the smallest element (l) zero and then use that to make other elements zero as well. While we were making l zero (divide by 3k), we also multiplied some element by 3k and hence first we remove that. Then we just iterate over different powers of 3 (since they decide how many times we divide a number to make it zero) and find number of elements to divide by that number (the power tells us number of times this division is required)
In E,my submission fails at test case 1, where as it works well in my local machine as well as in online cpp shell. Can anyone let me know why it happened, Thanks.
using log function might be the case it create inconsistencies you could have used while loop to count same thing
Can anyone help me in G2? I am getting wrong answer Integer 0 violates the range [1, 1000] (test case 1) in Test 1. I even manually wrote exceptions, that if my variable is 0, output 1 instead. Still, I do not know why I am getting output 0 https://codeforces.me/problemset/submission/1999/275154103
275163443
Why my this solution for E giving me TLE? Isn't this only O(N)?
Thanks :)
O(N) will give TLE as there is no limit across all test cases. So, O(N) will take t*(r-l)= 2*10^9. Think. you can do much better than O(N)
why did i use RMQ on E? :skull:
In this contest, the question was just about if else and due to which it matched with some one. How can you declare that I was cheating in that contest. Please look into this matter. Thanks mine : aritg/274372105 23CS02002/274399627
getting the detail right on problem b was so easy yet frustrating
Alternative solution for A, write an if statement for each of the 90 possible inputs. This is very efficient because you don't use a for loop or any other costly operation such as division or modulo. My code: https://codeforces.me/contest/1999/submission/277148166
I screwed up so much on F during a virtual contest, only to realize I forgot an extra mod operation...
Worst question B