We hope you enjoyed the contest! Thank you for participating! This is our second official round on Codeforces, so we would be happy to hear your feedback in the comments and in the mini-survey below.
Idea: Wileyne; developer: Wileyne
#include <bits/stdc++.h>
using namespace std;
void solve() {
string a, b, c;
int n, m;
cin >> n >> a;
cin >> m >> b >> c;
string add_left = "";
for (int i = 0; i < m; ++i) {
if (c[i] == 'V') {
add_left += b[i];
} else {
a += b[i];
}
}
reverse(add_left.begin(), add_left.end());
cout << add_left + a << '\n';
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Idea: fstilus; developer: fstilus
If Vadim appends $$$k$$$ zeros to the number $$$x$$$, what will be the ratio between $$$n$$$ and $$$x$$$?
$$$n = x \cdot (10^k + 1)$$$
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long r;
cin >> r;
long long d = 11;
vector <long long> ans;
while (r >= d) {
if (r % d == 0)
ans.push_back(r / d);
d = (d - 1) * 10 + 1;
}
cout << (int)ans.size() << '\n';
for (int i = (int)ans.size() - 1; i >= 0; --i)
cout << ans[i] << ' ';
cout << '\n';
}
return 0;
}
2132C1 - The Cunning Seller (easy version)
Idea: fstilus; developer: KotlechkovEgor
Does it make sense to use the same type of deal more than 2 times?
The solution with the minimum number of deals is unique.
Ternary numeral system.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <long long> cost;
long long c = 3;
long long cnt = 1;
for (int i = 0; i < 21; ++i) {
cost.push_back(c);
c = 3 * c + cnt;
cnt *= 3;
}
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long min_k = 0;
long long min_cost = 0;
int sz = 0;
while (n) {
min_k += n % 3;
min_cost += (n % 3) * cost[sz];
n /= 3;
sz++;
}
cout << min_cost << '\n';
}
return 0;
}
2132C2 - The Cunning Seller (hard version)
Idea: Boodoochai; developer: KotlechkovEgor
What is more profitable: 3 deals for $$$3^x$$$ watermelons each, or 1 deal for $$$3^{x+1}$$$ watermelons?
Recall the solution to problem C1.
#include <bits/stdc++.h>
using namespace std;
int main() {
vector <long long> cost;
long long c = 3;
long long cnt = 1;
for (int i = 0; i < 21; ++i) {
cost.push_back(c);
c = 3 * c + cnt;
cnt *= 3;
}
int t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
vector <long long> tr;
long long min_k = 0;
while (n) {
tr.push_back(n % 3);
min_k += n % 3;
n /= 3;
}
if (min_k > k) {
cout << -1 << '\n';
continue;
}
k -= min_k;
k /= 2;
for (int i = (int)tr.size() - 1; i >= 1; --i) {
if (tr[i] <= k) {
tr[i - 1] += 3 * tr[i];
k -= tr[i];
tr[i] = 0;
} else {
tr[i - 1] += k * 3;
tr[i] -= k;
break;
}
}
ll an = 0;
for (int i = (int)tr.size() - 1; i >= 0; --i)
an += cost[i] * tr[i];
cout << an << '\n';
}
return 0;
}
Idea: fstilus; developer: fstilus
Find out which number the $$$k$$$-th digit belongs to in the infinite sequence. Let this number be $$$n$$$. Instead of analyzing the sequence, we can calculate the sum of the sums of the digits of all integers from $$$0$$$ to $$$n - 1$$$ and add to this the sum of the required digits of the number $$$n$$$.
Mentally add leading zeros to all numbers from $$$0$$$ to $$$n - 1$$$ so that their lengths become the same. The sum of the sums of the digits will not change, but it will be more convenient to determine the formulas in this case.
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long k;
cin >> k;
long long cur = 9, len = 1;
while (k - cur * len > 0) {
k -= cur * len;
cur *= 10;
len++;
}
string s = to_string(cur / 9 + (k - 1) / len);
long long ans = 0;
for (int i = 0; i < (k - 1) % len + 1; i++)
ans += s[i] - '0';
long long pr_s = 0;
for (int i = 0; i < s.length(); i++) {
int curd = s[i] - '0';
if (curd)
ans += curd * (len - 1) * cur / 2 + curd * (2 * pr_s + curd - 1) / 2 * cur / 9;
cur /= 10, len--;
pr_s += curd;
}
cout << ans << '\n';
}
return 0;
}
2132E - Arithmetics Competition
Idea: EzikBro; developer: EzikBro
If we know how many specific cards Vadim should take, and how many should take Kostya (for example when $$$x + y = z$$$). How can we then obtain the maximum possible sum?
We need to quickly compute the sum of the $$$x$$$ maximum elements in array $$$a$$$ and the sum of the $$$y$$$ maximum elements in array $$$b$$$. What should we do to be able to compute this for arbitrary $$$x$$$ and $$$y$$$ in $$$O(1)$$$?
How can we solve this problem if $$$x = n$$$ and $$$y = m$$$ — that is, each person can choose any number of cards, as long as their total is $$$z$$$?
If $$$x = n$$$ and $$$y = m$$$, then the optimal set of $$$z$$$ cards consists of $$$x'$$$ maximum cards from Vadim and $$$y'$$$ maximum cards from Kostya (where $$$x' + y' = z$$$). How should we adjust this optimal answer if the optimal number for Vadim or Kostya exceeds their limits (that is, if $$$x \lt x'$$$ or $$$y \lt y'$$$)?
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector <int> a(n), b(m);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
cin >> b[i];
sort(a.rbegin(), a.rend());
sort(b.rbegin(), b.rend());
vector <long long> pa(n + 1), pb(m + 1);
for (int i = 0; i < n; i++)
pa[i + 1] = pa[i] + a[i];
for (int i = 0; i < m; i++)
pb[i + 1] = pb[i] + b[i];
vector <pair <int, int>> ans(n + m + 1);
int l = 0, r = 0;
for (int i = 1; i < ans.size(); i++) {
if (l < n && r < m) {
if (a[l] < b[r])
r++;
else
l++;
}
else if (l == n)
r++;
else if (r == m)
l++;
ans[i] = { l, r };
}
for (int i = 0; i < q; i++) {
int x, y, z;
cin >> x >> y >> z;
l = ans[z].first, r = ans[z].second;
if (l > x)
cout << pa[x] + pb[z - x] << '\n';
else if (r > y)
cout << pa[z - y] + pb[y] << '\n';
else
cout << pa[l] + pb[r] << '\n';
}
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector <int> a(n), b(m);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < m; i++)
cin >> b[i];
sort(a.rbegin(), a.rend());
sort(b.rbegin(), b.rend());
vector <long long> pa(n + 1), pb(m + 1);
for (int i = 0; i < n; i++)
pa[i + 1] = pa[i] + a[i];
for (int i = 0; i < m; i++)
pb[i + 1] = pb[i] + b[i];
for (int i = 0; i < q; i++) {
int x, y, z;
cin >> x >> y >> z;
int l = max(0, z - y);
int r = min(z, x);
while (l + 2 < r) {
int m1 = (l + l + r) / 3;
int m2 = (l + r + r) / 3;
if (pa[m1] + pb[z - m1] > pa[m2] + pb[z - m2]) {
r = m2;
} else {
l = m1;
}
}
long long s = 0;
for (int i = l; i <= r; i++)
s = max(s, pa[i] + pb[z - i]);
cout << s << '\n';
}
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
}
2132F - Rada and the Chamomile Valley
Idea: Friendiks, Wileyne; developers: Friendiks, Wileyne
Recall or learn what bridges are and understand how this is related to the lanes that lie on all paths from $$$1$$$ to $$$n$$$.
How to find all the bridges that lie on every path from $$$1$$$ to $$$n$$$?
Solve the problem for the case where $$$n = q$$$ and $$$c_i = i$$$.
How to find the nearest vertex from a given set of vertices $$$S$$$ for each vertex?
#include <bits/stdc++.h>
using namespace std;
vector <vector <int>> g;
vector <bool> used;
vector <int> d, h;
vector <pair <int, int>> edges;
vector <int> cut, path;
int mark = 1;
void dfs(int v, int p, int n) {
used[v] = true;
d[v] = h[v] = (p == -1 ? 0 : d[p] + 1);
if (v == n) mark = 0;
for (auto e : g[v]) {
int to = edges[e].first + edges[e].second - v;
if (to == p) continue;
if (used[to])
h[v] = min(h[v], d[to]);
else {
path[e] ^= mark;
dfs(to, v, n);
path[e] ^= mark;
h[v] = min(h[v], h[to]);
if (h[to] > d[v]) cut[e] = 1;
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
g.assign(n + 1, {});
used.assign(n + 1, false);
d.assign(n + 1, 0);
h.assign(n + 1, 0);
edges = {};
cut.assign(m, false);
path.assign(m, false);
mark = 1;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
edges.push_back({a, b});
g[a].push_back(i);
g[b].push_back(i);
}
dfs(1, -1, n);
vector <int> ans(n + 1, n + m);
vector <int> dist(n + 1, n + m);
queue <int> q;
for (int i = 0; i < m; ++i) {
if (cut[i] && path[i]) {
ans[edges[i].first] = min(ans[edges[i].first], i + 1);
ans[edges[i].second] = min(ans[edges[i].second], i + 1);
if (dist[edges[i].first]) {
dist[edges[i].first] = 0;
q.push(edges[i].first);
}
if (dist[edges[i].second]) {
dist[edges[i].second] = 0;
q.push(edges[i].second);
}
}
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto e : g[v]) {
int to = edges[e].first + edges[e].second - v;
if (dist[to] > dist[v] + 1) {
dist[to] = dist[v] + 1;
ans[to] = ans[v];
q.push(to);
}
else if (dist[to] == dist[v] + 1)
ans[to] = min(ans[to], ans[v]);
}
}
int Q;
cin >> Q;
while (Q--) {
int v;
cin >> v;
if (ans[v] == n + m) cout << -1;
else cout << ans[v];
if (t || Q) cout << '\n';
}
}
return 0;
}
Idea: fstilus; developers: fstilus, pskobx
There is no point in adding new symbols both above and below the original table simultaneously. Similarly, there is no point in adding symbols both to the left and to the right of the table at the same time.
The center of the optimal answer table lies within the original one.
We do not need to construct the answer; it is sufficient to check that an answer exists for the current center. Think about how to do this.
An answer exists for the current center if the subtable, which is the intersection of the original table and the one rotated around the center by $$$180^{\circ}$$$, becomes identical to the original when rotated by $$$180^{\circ}$$$. This subtable must contain at least one of the corners of the original table.
The checking of centers can be replaced by the checking of subtables that contain at least one of the corners of the original table.
To check the subtables to see if they become identical to the original when rotated by $$$180^{\circ}$$$, we can use hashing.
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int P[2] = { 107, 61 };
int BP[2];
long long bin_pow(long long a, int n) {
long long ret = 1;
while (n) {
if (n & 1) ret = (ret * a) % MOD;
a = (a * a) % MOD;
n >>= 1;
}
return ret;
}
inline int add(int a, int b) {
int res = a + b;
if (res >= MOD) return res - MOD;
return res;
}
inline int sub(int a, int b) {
int res = a - b;
if (res < 0) return res + MOD;
return res;
}
inline int mult(int a, int b) {
long long res = (long long)a * b;
if (res >= MOD) return res % MOD;
return res;
}
int main() {
int t;
cin >> t;
BP[0] = bin_pow(P[0], MOD - 2);
BP[1] = bin_pow(P[1], MOD - 2);
vector <int> pows[2];
vector <int> bpows[2];
for (int j = 0; j < 2; j++) {
pows[j].resize(1e6, 1);
bpows[j].resize(1e6, 1);
for (int i = 1; i < 1e6; i++) {
pows[j][i] = mult(pows[j][i - 1], P[j]);
bpows[j][i] = mult(bpows[j][i - 1], BP[j]);
}
}
while (t--) {
int n, m;
cin >> n >> m;
vector <string> f(n);
for (int i = 0; i < n; i++)
cin >> f[i];
vector <vector <int>> hash(n + 2, vector <int> (m + 2, 0));
vector <vector <int>> bhash(n + 2, vector <int> (m + 2, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int cur = mult((f[i - 1][j - 1] - 'a' + 1), mult(pows[0][i - 1], pows[1][j - 1]));
hash[i][j] = add(sub(add(hash[i - 1][j], hash[i][j - 1]), hash[i - 1][j - 1]), cur);
}
}
for (int i = n; i >= 1; i--) {
for (int j = m; j >= 1; j--) {
int cur = mult((f[i - 1][j - 1] - 'a' + 1), mult(pows[0][n - i], pows[1][m - j]));
bhash[i][j] = add(sub(add(bhash[i + 1][j], bhash[i][j + 1]), bhash[i + 1][j + 1]), cur);
}
}
auto isp = [&](int x1, int y1, int x2, int y2) {
int hsh = add(sub(sub(hash[x2][y2], hash[x1-1][y2]), hash[x2][y1-1]), hash[x1-1][y1-1]);
int bhsh = add(sub(sub(bhash[x1][y1], bhash[x2+1][y1]), bhash[x1][y2+1]), bhash[x2+1][y2+1]);
hsh = mult(hsh, mult(bpows[0][x1 - 1], bpows[1][y1 - 1]));
bhsh = mult(bhsh, mult(bpows[0][n - x2], bpows[1][m - y2]));
return hsh == bhsh;
};
int mn = n * m * 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (isp(1, 1, i, j)) mn = min(mn, (2 * n - i) * (2 * m - j));
if (isp(1, j, i, m)) mn = min(mn, (2 * n - i) * (m + j - 1));
if (isp(i, 1, n, j)) mn = min(mn, (n + i - 1) * (2 * m - j));
if (isp(i, j, n, m)) mn = min(mn, (n + i - 1) * (m + j - 1));
}
}
cout << mn - m * n << '\n';
}
return 0;
}








I found the solution for C2 1 minute after the contest ended! I am so sad :(
++ :(
Can you tell me your method(s) to solve the C2? I don't understand their solution.Thanks
For C2, every time you break a deal down into 3 smaller ones, you are adding 2 more deals to the count of deals meaning that the amount of times you can break a deal down is (k — min_k) / 2. Their solution for C2 was basically trying to break down all the deals that they could using the
for (int i = (int)tr.size() - 1; i >= 1; --i)loop which could either run until the end or break when k < tr[i] (a.k.a running out of times they could break down a deal).In fact, you should buy as less as you can in one deal. but, the limit of k makes you must make some large deals. So, you should use as less as you can deals to fit the limits by greedy. and an important step is to find out that break a great deal is better than break a little deal.So you break the largest deal into three small deal and the count of deal plus 2.Repeat it untill reach the limit
Greedly to buy smaller groups,greedly to make biggest smaller.
Same man, I regret sitting for the contest 15 min late
I was quite close as well, but, I'm glad I didn't give up!
Thanks for the fast editorial and the contest... very mathematical
my brain melted just because of B, C and D being math
Thank you for the contest! The problems felt very new and refreshing.
This contest is below the average not good and not bad.
Amazing contest! C2 was really fun.
how could you say this
Undoubtedly, Problem C2 is very well-designed. It leverages the code from C1 to check the existence of a solution, then simply adding a greedy strategy solves it.
it was painful, but it's a good problem
F is a piece of cake if you've solved this
what's with the cartoons?
Thank you for the contest ! , although I did unrated , but definitely one of my most favorite Div3's .
Kudos to authors.
HELP
I did exact same thing as mentioned in editorial for problem F but it gives WA,any help is appreciated Kogut_Ivan .
Submission — 334939521
F was easy but i missed the output format i thought if there are no valid lanes then just output single line -1 and move for another testcase:(
D is already online available: Geek for geeks cses
I am here after hearing that 2132E - Arithmetics Competition is practically equivalent to 2063D - Game With Triangles. As the coauthor of that problem, I must confirm that this is true. So you cannot fail to disappoint, huh...
I demand justice for ternary search solution for problem E! This problem is actually so fun to do ternary search with, as the moment you realize the function is linear brings so much satisfaction (from my 10-second long experience, it is indeed satisfactory)!
Is there a way to filter submissions on programming language? I would like to try to hack submissions, but most of them are in C++ and I code in Python.
yes , just open the hack page and use the filter provided.
C2 was really fun! Happy for being able to solve it just before the contest ended.
E looks interesting, will try to solve it later..
D is similar like digit queries of CSES problemset. I already solved digit queries still not able to solve the d problem sad :(
Today's contest so bad for me , i don't know what happened to me today , i don't have words..
can any one tell why this wrong for last test case 260010000
F was really amazing!
The problems were too mathematical and time consuming. Personally, I did not like the contest.
Who is supposed to know or think this: If Vadim appends k zeros to the number x, what will be the ratio between n and x? I'm not doing math olympiad.
Actually it's very easy to find the solution once one think like a mathematician.
Math forces. I did e and f but not c2 and d, hope to see another div 3 soon ☺️
Can someone explain why ternary search works in E but not binary search?
i did bin search and it worked https://mirror.codeforces.com/contest/2132/submission/334906002 is it hack-able tho?
I used binary search 334924908
Idea : Sort both arrays in descending order and try to find
iandjsuch thata[i]andb[j]are as close as possibleBinary search works. choose a 'x' that all elements you choose from array a and b is >= x. And do binary search on answer for that.
I didn't understand "C1", statement and solution, why !
see c1 is like you have to sell n watermelon but the scene you know the cost for selling 3^x melons so what you have to you have to break or decompose the number to 3 power
how i approach may be wrong if n=20 so you write it like nearest factor of 3 and some remainder which will make 18 +2 ok now you have to decompose the 18 to 3 power
you can write like 3^2*2+2-> 3^2*2+3^0+3^0 ok this is how you can write 20 now just compute values
In C1, the minimum number of transactions is equivalent to representing the original number in ternary
Please somebody explain me whats wrong in my solution for B 334909623, please help
С2 is so nice
in C2 why do we always transfer from tr[i] to tr[i-1], in that order. I mean why cant we start from tr[0] to tr[i-1] and transfer from tr[i] whenever possible? this basically fails on the 180th number of tc 2 and i have no clue why.
The difference between the values grows proportionally to x, and the greater x is, the greater the difference between the costs of buying 3^i watermelons and 3^(i-1). So, we want to minimize cost => we want to transfer from greater values.
yeah but what i meant was why cant i directly go from 3^i to 3^0 if k permits. this too has become clear because we gain more from killing off the highest powers of 3 than by creating more lower powers of 3
Lets see the case when $$$n = 20$$$ and $$$k=14$$$.
If you get the base 3 representation of $$$n$$$, you obtain $$$(20)_{10} =(202)_3$$$. This representation is really useful, because you get the minimum number of deals $$$(2+0+2) = 4$$$, that you need to obtain exactly $$$20$$$ watermelons (answer of C1). Then the cost will be $$$((2\cdot(3^3 + 2\cdot3^1)) + (0\cdot(3^2 + 1\cdot3^0)) + (2\cdot(3^1 + 0\cdot3^{-1}))) = 72$$$.
Now, you have to note that $$$(202)_3 \equiv (2\cdot3^2 + 0\cdot3^1 + 2\cdot3^0)$$$, but you can also represent the $$$(2\cdot3^2)$$$ as $$$(1\cdot3^2 + 3\cdot3^1)$$$ or as $$$(0\cdot3^2 + 6\cdot3^1)$$$ or even as $$$(0\cdot3^2 + 0\cdot3^1 + 18\cdot3^0)$$$. In fact, you can get multiples representation of the same number following this format and this representation tells you how many deals you need to obtain it, and with that representation you also can get the cost.
Finally, it's important to see that if you represent a power of 3 with a smaller one, you obtain a smaller cost. Since we want to minimize the cost, then you want to reduce the bigger power of 3 in your base 3 representation. E.g., the cost of $$$(2\cdot3^2 + 0\cdot3^1 + 2\cdot3^0)$$$ was $$$72$$$ but the cost of $$$(1\cdot3^2 + 3\cdot3^1 + 2\cdot3^0)$$$ is $$$69$$$. Note how from one representation to the other, you used $$$2$$$ more deals $$$(1+3+2) = 6$$$.
The minimum cost will be obtained if you buy the watermelon using just deals of type $$$3^0$$$, so yes you can move from the $$$3^i$$$ to the $$$3^0$$$, but this is not always possible. Then, the better approach is to simulate the moving process in order to ensure the best answer.
I will simulate the process of the algorithm below:
REP____| k__| cost
2_0_2___| 4__| 72
1_3_2___| 6__| 69
0_6_2___| 8__| 66
0_5_5___| 10_| 65
0_4_8___| 12_| 64
0_3_11__| 14_| 63 ----- FINNISH
0_2_14__| 16_| 62 ----- k NOT BIG ENOUGH
0_1_17__| 18_| 61 ----- k NOT BIG ENOUGH
0_0_20__| 20_| 60 ----- k NOT BIG ENOUGH
This algorithm is slow, and will give TLE (check here: 334956210), but you can accelerate easily just computing the decrement amount that you have to do in each power of 3 instead of simulating the entire process as you can see here: 334950832.
nice
If the buyer has an upper limit of k, and they can still make
extra deals, then it is always optimal to convert
(Proof is given in the editorial).
Example:
Here the seller sells in powers of $$$3$$$.
So total:
Thus exactly 3 deals (= min_k).
Now if the buyer is allowed more deals ($$$k \gt \text{min}_k$$$):
tr.That’s why we go right → left: to break a larger power into smaller powers (ex — Convert 1 deal of 3^3 into 3 deal of 3^2)
By doing this, the total cost reduces, which is our objective.
I hope i can solve 4 problems at least in next Div3!
Learned a lot more about CF contests, just need to do more problems, need to get more better and enjoy it more, hopefully in the next div 4 or div 3 I get better :). I enjoyed it being mathematical but realized python is limiting me, need to start using C++ :)
Thanks for the contest, hoping yall have a great day :)
thanks guys, good work
I have Just fully understanded how to buy watermelons in k deals :(
E and C2 are interesting!But I don't like D.
Hello! If my rating is 1400 right now, what number of tasks in a contest should i ac so that it won't drop the rating? Thx
5 (Div 3) 3 (Div 2)
334980092 [C2]Can someone help check why my code wrong on test2?
Simpler solution to D:
Binary search for the largest $$$L$$$ such that the total number of digits in $$$1, ..., L$$$ is at most $$$k$$$. Then, compute the sum of digits of all numbers in $$$1, ..., L$$$ as well as the partial piece of $$$L + 1$$$.
The number/sum of digits in $$${1, ..., N}$$$ are standard problems. The latter can be calculated via a digit-dp like approach.
Code: 334983266
thnx dude:)
in C2, why are we decreasing the value of k after doing the minimum number of deals (in the solution code what is the sense of the line k-=min_k; k/=2; ?)
I think C2 s a little awful in quality. It does not conclude high-qualitied thinking but is tricky in details, which is very annoying.
P.S. I am a Chinese student, my English is not good, please understand.
A good contest! I love it.
Was so close to solve the D but couldn't figure out how to handle a number so big
Why should we do k /= 2 in C2 solution?
If you move a 3^x deal, you need three 3^(x — 1) deals. -1+3=2, so we should do k /= 2 in C2 solution
Thank you so much
Good C2 but bad F and G.
My solution for B using Binary search.
For problem C1 the stated solution in the editorial for lets say 11 watermelons would be the ternary number representation, which is 3^2 + 3^0 + 3^0 = 11, that would be 3 deals. But we could actually buy 12 (1 more than necessary) with 1 less deal, make it 2 deals, with 3^2 + 3^1 = 12 watermelons. So is the editorial solution wrong or am i missing something ?
In both C1 and C2 you have to buy exactly $$$n$$$ watermelons.
its specified in the qstn that u should not buy more than n
Wileyne
Note that this function is convexIsn't the function concave ?Yes I also think so.
In problem-C1, I found attached statement ambiguous
The buyer is in a hurry and has therefore turned to you to determine the minimum number of coins he must pay the seller for n watermelons, considering that he will make the least possible number of deals.
Which parameter should be minimized first? no of deals or cost
Because as per formula, cost(3^(x+1)) > cost(3*3^x)
cost(3^(x+1))=3^(x+2)+(x+1)*3^x
cost(3^x)=3^(x+1)+x*3^(x-1)
cost(3^(x+1))-3*cost(3^x)=3^x
This means if we increase no of deals cost will reduce but if we reduce no of deals cost will increase
The statement, "considering that he will make the least possible number of deals", defines the number of deals explicitly. That is, you need to consider the minimum number of deals in general. From there, minimize the amount that will be paid.
Note: The minimum number of deals is sum of digits in the base-3 representation of the given number.
Got it Thanks!!
Why do I get Wrong Answer on test 97 when I submit the author's code (problem G)?
.
Considering that the base set and modulo in the editorial's solution is fixed, I would guess that test 97 is a hack case.
The memory used to allocate vectors is way more than the actual table's content. My solution used nearly 300MB of memory while the tables stores only 20MB of data.
Feels like I’m doing a math olympiad, not a Codeforces contest
dumb math
great editorial!
The pattern of the contest was not up to the mark. Problems A,B,C1 were extremely easy and the gap between C1 and C2 was huge. As a result, both newbies and even many specialists ended up solving till C1. Please try to make the gaps between problems uniform!
https://hsin.hr/coci/archive/2006_2007/contest1_tasks.pdf
solved.
2132D - From 1 to Infinity Can I get an easy explanation or how did we derive that formula used to calculate the sum, if there is any article or any explanation then please tell me.
Hey guys. Can you tell me is my idea for solving C2 correct?
First of all, let's find this observation that: 3^18 > 10^9 and 3^18 is the maximum number lower than $$$10^9$$$. It will help us handle '-1'.
Instead of going from "large to small" I go from "small to large". See the examples below:
$$$n=20, k=14$$$
1-st step: maximum number of deals is 20 (let's ignore $$$k$$$). Why? We can sell 20 watermelons for 60 coins using $$$3^0 = 3$$$ coins per watermelon. This is the minimum number of coins we need to spend to get 20 watermelons.
2-nd step: but 20 deals is more than 14 deals so let's sell some group of watermelons. Here comes a binary search. We need to find the minimum number of watermelons we need to group to make number of deals less than k.
We're moving our pointers by following rules:
$$$r = mid$$$
$$$l = mid + 1$$$
$$$l = 0, r = 20 / 3 = 6$$$
$$$(0 + 6) / 2 = 3$$$. If we have 3 groups of 3 watermelons there will be more 3 * 3 = 9 watermelons ($$$3^1$$$) and 11 remaining watermelons ($$$3^0$$$). 11 + 3 = 14 deals and our checking function will return true so move the right pointer to 3 ($$$r = mid$$$).
$$$l = 0, r = 3$$$
$$$(3 + 0) / 2 = 1$$$ If we have 1 group of 3 watermelons there will be 1 * 3 = 3 watermelons ($$$3^1$$$) and 17 remaining watermelons ($$$3^0$$$). 17 + 1 = 18 deals and it's greater than $$$k$$$ so move left pointer to 2 (because l = mid + 1).
$$$l = 2, r = 3$$$
$$$(2 + 3) / 2 = 2$$$ If we change 2 watermelons there will be 2 * 3 = 6 watermelons ($$$3^1$$$) and 14 remaining watermelons ($$$3^0$$$). 2 + 14 = 16 deals and it's greater than k ($$$=14$$$) so move the left pointer to 3.
Because: l == r we need to stop our loop.
3-rd step: so we've found that we need to change 3 watermelons and because: $$$3 + 11 \lt = k$$$ then we are stopping our loop.
So the final answer is: $$$3 * 10 + 11 * 3 = 63 coins$$$
-1 case handling
If we change watermelons over and over and it exceeds 3^18 then it is impossible to make $$$n$$$ watermelons in under than $$$k$$$ deals.
Sorry for my bad english
the problem D is so hard that i can't understand ,anyone could help me?
F is such a great problem!! Loved it,although I have not read the tutorial yet but saw some solutions,I think everyone's doing the same thing as I did.
In the editorial for problem D, what do they mean by writing: "We will immediately add to the answer the sum of the first k digits of n", I mean if k is 35 for example, the number at the 35th digit is 22. So then "we will add to the answer the sum of the first 35 digits of 25" ? 25 has only 2 digits, what do they mean by that
Does someone understand the editorial for problem D and can maybe explain it to me ?
I solve problems in python and in problem B coudnt figure out whats wrong till the end. Turn out after 15-16 digits python dont store float number accurately and thats the only reason for me to get wrong answer.
Why my code gives wrong output for last test case in last three digits for c1
In the From 1 to Infinity how do we prove that when we convert n to a string iterate its of limited size and not of size of order 10^12 or something ?
If a unimodal function has a flat top, can the ternary search algorithm be applied on the integer domain only if the flat top occurs at the extremum point? I used to think that if there was a flat top, the ternary search could not be used.
It can be applied to that along with a few other forms of unimodal integer functions. You can search a unimodal integer function so long as it has strict inequality on one end and loose inequality on the other. In other words, it must be in one of these forms:
$$$f(1) \lt f(2) \lt \dots \lt f(k) \geq f(k+1) \geq f(k+2) \geq \dots$$$ $$$f(1) \leq f(2) \leq \dots \leq f(k) \gt f(k+1) \gt f(k+2) \gt \dots$$$ $$$f(1) \gt f(2) \gt \dots \gt f(k) \leq f(k+1) \leq f(k+2) \leq \dots$$$ $$$f(1) \geq f(2) \geq \dots \geq f(k) \lt f(k+1) \lt f(k+2) \lt \dots$$$
If we were to allow loose inequality on both ends, then getting the same value twice wouldn't tell us where we are relative to the maximum/minimum (whereas allowing it on strictly one end tells us that we are currently still on the same side of the maximum/minimum so long as we are comparing two consecutive spots during the ternary search). An example of an implementation which supports the aforementioned ternary search property can be found on kactl.
Your point is very enlightening, thank you!
In problem E, the writer says "This will be the optimal answer because all the cards taken from array a will be at least as large as all the cards that have not yet been taken from array b , meaning there is no point in making additional swaps of cards from one array to another." Can any explain this? I don't understand.
Alright how the hell is this a div 3 competition?