Thank you for participating!
1926A - Vlad and the Best of Five
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() {
string s;
cin >> s;
int a = 0, b = 0;
for (char c : s) {
if (c == 'A') {a++;}
else {b++;}
}
cout << (a > b ? 'A' : 'B') << '\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: mesanu
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <cassert>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<string> g;
for(int i = 0; i < n; i++)
{
string s;
cin >> s;
g.push_back(s);
}
bool triangle = false;
for(int i = 0; i < n; i++)
{
int cnt = 0;
for(int j = 0; j < n; j++)
{
if(g[i][j] == '1')
{
cnt++;
}
}
if(cnt == 1)
{
triangle = true;
}
else if(cnt > 1)
{
break;
}
}
reverse(g.begin(), g.end());
for(int i = 0; i < n; i++)
{
int cnt = 0;
for(int j = 0; j < n; j++)
{
if(g[i][j] == '1')
{
cnt++;
}
}
if(cnt == 1)
{
triangle = true;
}
else if(cnt > 1)
{
break;
}
}
if(triangle)
{
cout << "TRIANGLE" << endl;
}
else
{
cout << "SQUARE" << endl;
}
}
int32_t main(){
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
1926C - Vlad and a Sum of Sum of Digits
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 res[MAX];
int S(int x) {
int res = 0;
while (x) {
res += (x % 10);
x /= 10;
}
return res;
}
void solve() {
int x;
cin >> x;
cout << res[x] << '\n';
}
int main() {
res[0] = 0;
for (int i = 1; i < MAX; i++) {
res[i] = res[i - 1] + S(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;
void solve() {
int n; cin >> n;
map<int, int> cnt;
int ans = 0;
for(int i = 0, x; i < n; ++i) {
cin >> x;
if(!cnt[x]) ++ans, ++cnt[((1 << 31) - 1) ^ x];
else --cnt[x];
}
cout << ans << "\n";
}
main() {
int t = 1; cin >> t;
while(t--) {
solve();
}
}
1926E - Vlad and an Odd Ordering
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 n, k;
cin >> n >> k;
vector<int> v;
while (n) {
v.push_back((n + 1) / 2);
n /= 2;
}
int tot = 0, pow2 = 1;
for (int x : v) {
if (tot < k && k <= tot + x) {
cout << pow2 * (2 * (k - tot) - 1) << '\n';
return;
}
tot += x;
pow2 *= 2;
}
}
int main() {
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;
vector<pair<int, int>> odd, even;
bool valid(int gc[7][7], bool odd) {
for (int r = 1; r < 6; r++) {
for (int c = 1; c < 6; c++) {
if (gc[r][c] && ((r + c) % 2 == odd)) {
if (gc[r - 1][c - 1] && gc[r - 1][c + 1] && gc[r + 1][c - 1] && gc[r + 1][c + 1]) {
return false;
}
}
}
}
return true;
}
bool check(int g[7][7], int flips_left, int idx, vector<pair<int, int>>& vec, int valid_val) {
if (flips_left == 0) {
return valid(g, valid_val);
}
if (idx == vec.size()) {
return false;
}
bool ok = false;
ok |= check(g, flips_left, idx + 1, vec, valid_val);
g[vec[idx].first][vec[idx].second] ^= 1;
ok |= check(g, flips_left - 1, idx + 1, vec, valid_val);
g[vec[idx].first][vec[idx].second] ^= 1;
return ok;
}
void solve() {
int g[7][7];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
char c;
cin >> c;
g[i][j] = (c == 'B');
}
}
int res = 0;
for (int i = 0; i <= 4; i++) {
if (check(g, i, 0, odd, 1)) {res += i; break;}
}
for (int i = 0; i <= 4; i++) {
if (check(g, i, 0, even, 0)) {res += i; break;}
}
cout << res << '\n';
}
int main() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if ((i + j) % 2) {
odd.emplace_back(i, j);
}
else {
even.emplace_back(i, j);
}
}
}
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
}
1926G - Vlad and Trouble at MIT
Idea: mesanu
Tutorial
Tutorial is loading...
Solution coded by Dominater069, thanks!
Solution
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
int n;
const int N = 1e5 + 69;
int dp[N][3];
string s;
vector <int> adj[N];
void dfs(int u){
// dp[u][0] = nothing open
// dp[u][1] = P open
// dp[u][2] = S open
dp[u][0] = INF;
if (s[u] != 'S') dp[u][1] = 0;
else dp[u][1] = INF;
if (s[u] != 'P') dp[u][2] = 0;
else dp[u][2] = INF;
int tot = 0;
for (int v : adj[u]){
dfs(v);
dp[u][1] = dp[u][1] + min({dp[v][1], dp[v][2] + 1, dp[v][0]});
dp[u][2] = dp[u][2] + min({dp[v][2], dp[v][1] + 1, dp[v][0]});
tot += dp[v][0];
}
if (s[u] != 'C') tot = INF;
dp[u][0] = min({tot, dp[u][1] + 1, dp[u][2] + 1});
//cout << u << " " << dp[u][0] << " " << dp[u][1] << " " << dp[u][2] << "\n";
}
void Solve()
{
cin >> n;
for (int i = 1; i <= n; i++) adj[i].clear();
for (int i = 2; i <= n; i++){
int x; cin >> x;
adj[x].push_back(i);
}
cin >> s; s = "0" + s;
dfs(1);
cout << min({dp[1][0], dp[1][1], dp[1][2]}) << "\n";
}
int32_t main()
{
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
return 0;
}
For Problem-F, I have a bitmask dp solution: 247350477
I am interested to know how you formed intitution for bitmask dp when it looks like a graph based problem at first.
Firstly, I thought if we precalculate answers for all matrixes at the beginning, then we can solve queries in $$$O(n^2)$$$ which equals reading matrix.
But how can we precalculate answers for all matrixes. Like in the editorial, we can separate matrix into 2 sets; which one of them has 25 elements, other's 24. How can we calculate answers for all matrix designs in one set.
And then I reduced problem to a bitmask DP problem. And I found a classicial DP solution:
Try to show possible matrixes as a masks, which black square is 1, else 0. Then $$$f(mask)$$$ will show answer for $$$mask$$$.
We have 2 easy cases:
It is a good matrix (every black square doesn't have 4 diagonal black square) and $$$f(mask) = 0$$$: We can check it in $$$O(bit \ number \ of \ mask)$$$.
It isn't a good matrix, and suppose we transform $$$k.$$$ black square to white: $$$f(mask) = \min_{\forall k \in mask} f(mask \otimes 2^k) + 1$$$ And our case will has a same complexity as 1. case.
Then we will have $$$2^{25}$$$ mask for first set, and $$$2^{24}$$$ for second set. And final complexity will be $$$O((2^{25} + 2^{24}) \times 25 + (T \times 49))$$$ or $$$O(2^{\frac{N^2}{2}} \times N/2 + T)$$$. Nearly $$$2 \times 10^9$$$ which is in TL. But it is too close to TL so it can get TLE if your constant is big.
Note: You can fast your solution with magical bitwise operations.
Also sorry for my poor English.
Wow, amazing approach, thanks for sharing. It helped a lot.
great solution , can you share any resources from where I can learn it?
Usaco Guide Bitmask DP Tutorial may be helpful.
Thanks
I tried hard to solve F using bipartite matching but I failed. Does F can't be solved by bipartite matching? I think it can be solved by find minimum vertex cover with good network modeling. Plz help me.
I think F is very difficult to be solved by flow (network modeling).
I tried to find a modeling of minimum cut, but I found that it's difficult to produce by flow that for an illegal black cell, we need only to change one cell's color to make it legal. In other words, for a black cell that is not legal, we need to manipulate at least one other cells to make it legal. The "at least one" operation is difficult for network flows.
But perhaps there is some subtle transformation in this problem that eliminates the "at least" condition. I'm looking forward the subtle way to solve it expectantly.
hey how and where do you practice < I saw you're doing really good on contests.
Any tips !!!
My current level is solved 400+ on leetcode.
Practice as more as possible. CodeForces is not the main OJ on which I solve problem.
Which platform you use for practice and whats the good place to practice ?
The same as many Chinese CPer, I use Luogu mainly. You can wait for the international version of the site to use...
problem f is so bad i cant solve it
it's hard for div4 I agree
If you're looking for an easier bruteforce solution which worked!
Have a look at this
There's a much simpler solution for G.
Well done, I attempted the same DFS traversal, but never thought of changing C to P. What I did instead was to keep a count of P and S during traversal, and set either P or S to 0 once the wall is put. But haven't quite figured out how to make it AC. Although I have the same $$$cnt_s$$$ $$$cnt_p$$$ comparison :D
I did the same and got WA2, but I got lucky and found the bug 3 minutes before the contest ended :D
Could you explain why: when $$$cnt_s$$$ = $$$cnt_p$$$,We should change it to be the same as the parent.
I didn't do this, so I got WA2.But I don't understand why we have to do this.
thank you
you can try this hack 1 1 1 2 2 3 3 CCCPSPS anwser may be 2 or 3
Because if the parent is S and we changed the current node to P, this would take an extra operation.
For example :
We should change node 2 to S (total cost = 2), but if we change it to P, the total cost will be 3.
Amazing, I also tried same approach but couldn't figure out last condition. This last condition cnts==cntp is what I was missing out and getting WA on test case 2.
Thanks it helped me getting AC. My AC Solution 247695620
Can you explain if node == c why you gave hasS = 1 and hasP = 1 instead of giving hasS = 0 and hasP = 0?
I didn't do that. They are initially 0, and I change them based on which gives the minimum answer.
Can you attach the submission link ? I tried the same thing although i am getting WA on Test 3
247362381
I recorded myself live while solving A,B,C,D,E and thinking G. Hope it helps someone: https://www.youtube.com/watch?v=Zr2JbyTwq0A
Bro why you did it in live stream?
The stream was private during the contest. After the contest, I made it public. idk why there are so many downvotes. Many YouTube creators upload codeforces screencasts.
Sorry, i thought it was public during contest sorry for misunderstanding
By the way why so many downvotes its just private live and made public once the contest is over and do consider checking it out its actually helpful.
Why this solution get TLE? https://mirror.codeforces.com/contest/1926/submission/247402006 I can't think of a single reason, every operation in loops are constant
I solved it other way: https://mirror.codeforces.com/contest/1926/submission/247402620 But still curious why first one is TLE
Because of unordered_map blowing up.
This blog tells more about it.
Goddamn. Thank you.
Thank you. i had no idea why my ans was getting TLE.
I wrote this solution which just check each line if it has substring of "010" since k>1 that means a square is at least 2*2
if it has a "010" then it is a triangle else is square
its an easier approach
I have a bitmask dp solution for problem F.
first assume that B = 1 and W = 0, and change the grid accordingly.
Let's say the bit on cell (i, j) is bad iff
grid[i][j] = 1 and grid[i - 1][j - 1] = 1 and grid[i - 1][j + 1] = 1
.With that, let's define
dp[i][j][k]
to be the minimum number of flips considering the first i lines, the current mask on the i'th line is j, and k is a bitmask saying which bits from j are bad. Clearly, k is a submask of j, since the x'th bit from j can only be bad ifgrid[i][x] = 1
, this is important because now we can iterate on j and k in $$$O(3^N)$$$. Now we need to transition to i + 1. How? simple, just iterate on all possible masks, and check if they are valid.A mask is valid iff for all x, if x is bad than mask CANNOT have both (x — 1) and (x + 1) BOTH on. otherwise we would have that
grid[i - 1][x - 1] = 1 and grid[i - 1][x + 1] = 1 and grid[i + 1][x - 1] = 1 and grid[i + 1][x + 1] = 1
, which we cannot have.With some clever preprocessing of the masks, this dp can be achieved with a complexity of $$$O(N * 2^N * 3^N)$$$, with N = 7 and 200 testcases, this is fast enough
here's my solution
I solve problem C without precomputing in O(t*log10(n)).LOL 247319547
Do you have the math proof of that?? I couldn't understand
I was literally trying to formulate a thing like this but failed on it badly and messed up with my rating. going back to grey again :(
same. I literally wasted 20 minutes trying to find a formula, the 0.5 second bugged me.
me too,I didn't try to preprocess it
Can you explain the mathematical proof behind this?
Can you pls explain your solution? I also tried something similar but couldn't come up with one
I have a different approach with dp for problem F. First consider the following dp: dp[i][msk1][msk2].
i = current line
msk1 = what is the state of the last line
msk2 = what are the positions that, if we put two black cells in the diagonal in this line will make a X.
The transition for this dp will be: try every mask, see if the mask is possible and, if possible, calculate the cost of the mask.
The complexity of this would be O(7*2^7*2^7*2^7*7), and this is too slow for 200 test cases. But we can formulate the masks with a different way, take a number in base 3.
if the digit at pos j =
0: the pos j at last line was a W.
1: the pos j at last line was a B, and I can put two black cells in the diagonal.
2: the pos j at last line was a B, and I can't put two black cells in the diagonal.
This dp will be O(7*3^7*2^7*7), this will be sufficient for 200 test cases.
https://mirror.codeforces.com/contest/1926/submission/247403810
For problem D, rather than using XOR, I simply added the two numbers and checked to see if they were equal to
2^31 - 1
. Here was my submission: 247301401. I then sorted the array and used two pointers. Is the editorial solution superior to mine? If so, why?It's not superior. You also have a hard time accomplishing it in Python (
dict
can be TLE hacked). Sorting and 2 pointers is the safest option.Your solution probably will get better runtime than the editorial if implemented in C++, since the constant factor of map is bigger than traditional sorting.
The link to G in the tutorial is in russian for some reason.
have problem with reading problem statement E. wouldn't all cards with number from 1 to n appears? for example 8, it's clearly not odd, it can't be 2 * (any odd number) number, it can't be 3 * (any odd number), it can't be 4 * (any odd number). 8 never appears, thus constraint on k, n is not valid
8 * 1
The process does not stop at the step 4 -- in the step 8, you will have $$$8 \cdot 1 = 8$$$, because $$$1$$$ is an odd number.
so that was And so on, until all cards are laid down.. part now I can see thank you
So is there a polynomial solution for F? I tried modeling with network flow but failed, and I think such approach probably won't work.
I tried modeling with network flow but failed too lol
How can greedy approach ( 247465029 ) result in more deletions that optimal?
upd: think I understood why: it's a knapsack, in case anyone wondering
Okay, I've got it. Since we know from editorial that at most 4 squares are needed in each parity, we can replace knapsack with 4-sum (1-2-3-sums in fact) with total complexity $$$O(n^3)$$$. Slightly trollish but polynomial
Sorry, but this solution is nothing different from the one in the editorial. Your solution requires the fact that "at most 4 squares needed in each parity", however, this won't work for $$$n\ge 8$$$. So I had to say that this is not a true polynomial solution. It's indeed $$$\mathcal{O}(n^{\lfloor \frac{n}{2}\rfloor})$$$.
Indeed, should have checked it before. Thanks for pointing out.
While researching I found that low frequency set can be approximated, maybe that's the intended polynomial solution.
For F,my submission 247420824, why i add the dfs branch (the //add part),the result become worse. For example,
If I remove the comment symbols in the code and dfs by five cases, I get a result of 7.While following the code inside my link to dfs with three cases gives a result of 6.
Besides , What is the problem with the code inside my link and why can't AC.
Sorry for my poor English, can anyone help me ?
I found the problem, I forgot to add "return" in this place during dfs
This can lead to otherwise legal points that I still go to enumerate to modify its diagonal, resulting in a large answer. And the more cases of dfs, the more likely it is to lead to a large answer, which is what led to my question above.
How can I do hash collision on this solution?
use custom hash
I want to hack this not prevent it. Like I want understand how hack collision can be performed
You can checkout usaco guide,they have a section about generating hack cases for hack collisions.
Can somebody share the flow networks approach and solution for Problem G. I want to know how maxflow/mincut can find the solution. Thanks in Advance.
I don't get why dp is needed in G. Isn't it simple dfs?
I was thinking if we can solve G using min-cut.
Basically my idea was to combine all the 'P' to a node 0 in a new_graph and all the 'S' to a node n+1, which are basically acting as the source and sink respectively, and keeping the configuration of the 'C' vertices constant, which are acting as intermediate nodes. There can be multiple edges.
Then we calculate the max-flow, where each edge has a capacity of 1 which will be our final answer. But using Ford-Fulkerson gives $$$O(nm^2)$$$, thus giving TLE.
Can we use the fact that the capacity of each edge is 1 thus somehow reducing it to linear time ?
https://mirror.codeforces.com/contest/1926/submission/247492595
You need to use faster flow network
Thanks. It got AC using Dinic's algorithm. After you said faster flow network I googled and found out it takes less time for unit capacity $$$O(M√M)$$$.
Sorry, but since I didn't know about it before, I didn't really understand your implementation of it. Did you use the same?
I would like to mention my solution for F.
Observation 1: Grid can be divided into 2 independent sets of cells based on whether (i+j) is odd or even, with 25 and 24 cells in each.
Considering only black cells, we can brute force all possible flips in O(2^25 * 25). But this is slow.
Observation 2: Flipping cells from the grid boundary is not necessarily needed, we will still find an optimal flipping.
This reduces our cell count to 13 and 12. And the brute force is fast enough now.
Code
Sometimes your genius is just....astonishing.
My solution in C was $$$O(N + t)$$$ 247266041. If $$$n \le 10^6$$$ my solution will still work
But it contains precomputation too
A very simple solution for B. find the first instance of 1 in the grid. then check if the next cell is 1 and if the bottom cell is 1. if so then its square otherwise triangle.
for problem D i got TLE. I believe its o(n) can someone correct
Counter
behaves similarly tounordered_map
in C++. It can be TLE hacked wherec[k]
orc[e]
can be $$$O(n)$$$.What you can do is a
random.shuffle(a)
to make the hackers work harder :Dnoooo I didnt get hacked. I got TLE in contest itself.
They made the test case that will TLE if you use a
dict
or a similar hash based data type. They created a "hack" before the hacking phase.Try adding a
import random
andrandom.shuffle(a)
and you will see you will get AC (the test case that TLEs python will no longer work).ohhh thanks for the reply. Any idea why do they do this on purpose? And how do they achieve this exactly
They do this on purpose to make people come up with efficient algorithms that don't rely on hash tables. Of course, in C++ there's ordered
map
.For example, try solving:
Subarray Sums I: https://cses.fi/problemset/task/1660
Subarray Sums II: https://cses.fi/problemset/task/1661
You will be surprised :D
Here's a hack tutorial for Python and you can test it out yourself: https://mirror.codeforces.com/blog/entry/98994
You exploit the hash function to create collisions and make any resizing attempts still maintain collisions.
holy hell thanks for the articles
my c++ version also got TLE
Change unordered_map to map
Could you please explain the reason behind this?
This because unordered_map in c++ has bad implementation
Did you try using Fast IO? Python's Hash table uses Sip Hash. Not exactly similar to CPP's unordered_map but very strong. IMO hash table is not the cause for TLE.
https://mirror.codeforces.com/contest/1926/submission/247314271
Here's slow Python IO passing.
Hmm, this is interesting. I didn't come across such behaviour earlier. New learning for me.
Try this one: https://cses.fi/problemset/task/1661
They have a test case that purposefully TLEs a Python hashmap and if you switch to a $$$O(n \log n)$$$ solution, with same IO, it passes.
As a nooooooooooob,even I can't solve D and E lol Next I will record the results of each of my races. I believe I can be LGM before 2026!
For problem B, I have a solution in which I get the numbers of 1s in the first line the number 1 starts appearing.
If the numbers of 1s in the next line that has 1 is different (for example, line 1 has 3 ones, line 2 has 1 one) then it is a triangle, otherwise it is a square. However this solution doesn't work on test case 2 and I've not been able to find any edgecase in my mind that defies this logic: 247338503
Your description sounds correct, your code doesn't quite do that.
Thank you, my code didn't check for the case in which there're lines full of 0s below the shape, and it compared 0 number of 1s to the number of expected 1s to make a square (which was already completed) and flagged it as a triangle because these numbers are different.
Thank you for finding the test case!
Amazing work setters!! My Java Binary Trie Solution 247338014 with complexity O(30*N) TLEd on System Test which ideally should not. In contest it showed a runtime of 1091 ms , since it had a buffer of almost 1 sec , so i thought it was feasible. And again no surprise exact same code in C++ passes. 247463968 UPD : Got my AC back :)
Why didn't my rating change ( my rating was 381 before the contest ) even though I solved the first three problems ?
Have patience, it takes some time. Check now.
Alright thanks :)
how to solve F in polytime?
Why my solution gets TLE for Problem C? 247461107
This was my first Codeforces contest. Can anyone suggest study materials for dsa.
Can anyone explain this line of the editorial solution of Problem G:Vlad and Trouble at MIT?
dp[u][0] = min({tot, dp[u][1] + 1, dp[u][2] + 1});
I can not understand it. Please give some explanation.
I think this Div4 is harder than previous Div4, because problem F and G was very hard for pupil
Could you please explain the approach of D ?
Problem C: Why does it fail on Test #8 ? : Submission
`
include <bits/stdc++.h>
using namespace std;
int sumDigits(int n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; }
}
void solve(vector v, int n, vector ©) { map <int, int> mp; int ans = 0; int p = 0; for(int i = 1; i <= n; i++) { if(i == v[p]) { ans += sumDigits(i); mp[i] = ans; p++; continue; } else ans += sumDigits(i); }
}
int main() { ios::sync_with_stdio(0); cin.tie(0);
} `
first of all, put your code beetween ` ` next time so its more readable, and I think your code fails on this test case:
3
5
5
7
I'll leave it to you to figure it out as to why.
For Problem-F, we just need to check the cells marked as '@' below:
For the blue part, we need to check these cells:
So we just brute force from 0 to 4095.
For the red part, we need to check these cells:
So we just brute force from 0 to 511.
Here is my solution: 247559944.
Problem-D,Why does using unordered_map table time out?
For problem C I've a O(n) solution which runs in about 15 ms (c++) 247602474
How to deal with Problem C if n<=1e18. (Chinese student,Englishi is very poor...)
That means how to deal it with a O(t) solution. I think there is a solution which used math.
I don't sure but i think my solution here is O(log(n)) for each query
Thanks for your healping.Last night I solve Problem C with a O(log10 n * t) solution.This is my code:
And it could solve even though n<=10^1000 if t<=1^4
Oh,I'm sorry.The time complexity of my code is $$$O(t \log_{10} n)$$$.
guys just to justify the tags for C, i came up with a digit DP solution.
digit_dp
I wondered if there is a not hard brute-force approach for the problem F
I think problem F can further be optimized by considering only the inner 5x5 square (the same idea, just considering less squares for backtrack). This approach should be more than 10x faster.
I have upsolved problem F based on your idea by just checking the 5x5 square but using bit 1 in the generated binary numbers for painting (the 13 cells and 12 cells in the 5x5 square) rather than DFS.
248239875
For Problem D why my solution is giving TLE
CODE-:
I made video editorial for problem G.
Can anyone tell me why this is giving tle for E i did the same as in the editorial but i used binary search to fing no of elements we can take at each step am i missing something
For problem d:
for i in range(int(input())): n = int(input()) l = list(map(int,input().split()))
i am getting tle... how can i optimise it??
I found this solution helpful as a beginner programmer. However, I believe attaching a video solution would greatly benefit learners like me who might need a bit more visual guidance
Can anyone give test case for the case 1(no type of water flowing up) in case of Question G: Vlad and Trouble at MIT? I feel that the latter two cases are enough.
For Problem-C, I have a O(1) time complexity solution: 247367957. O(#digits(n))...better than the one in tutorial?
In the problem C, you don't need to use the sum of digits function at all. You can also use dp to find the answer. This removes the logn factor.See my code;
include <stdio.h>
include <stdlib.h>
int main() { int* a = (int*) malloc(sizeof(int) * (2000001)); for (int i = 0; i <= 9; ++i) { a[i] = i; } int c = 1; for (int i = 10; i <= 2000000; i++) { if(i / c == 10) { c = 10; } a[i] = a[i — c] + 1; } int b = (int*) malloc(sizeof(int) * (2000001)); b[0] = 0; for (int i = 1; i <= 2000000; ++i) { b[i] += b[i — 1] + a[i]; } int t; scanf("%d",&t); for (int i = 0; i < t; ++i) { int n; scanf("%d", &n); printf("%d\n", b[n]); } return 0; } If you have any doubt, feel free to ask.https://mirror.codeforces.com/contest/1926/submission/249616774
// Could someone try to identify the logic mistake for the B problem? I am just checking if any //row's sum is equal to 1 if yes it is a triangle else square.
include <bits/stdc++.h>
using namespace std;
int main() { ios_base::sync_with_stdio(false); cin.tie(0);
}
first you have to take ALL the inputs otherwise inputs of other tests may overlap, second what if there is only one 1 in the whole grid? this is square not triangle!
first you have to take ALL the inputs otherwise inputs of other tests may overlap could you please explain the above statement because I have used break so it shouldn't overlap right. Also, the input grid contains atleast 2*2 sqaure.
my bad, then you just have to take all the inputs, imagine with me that the shape it triangle, so in the first row of this triangle there is only one 1, your code just after getting that whole row prints triangle then break BEFORE you continue to take the coming rows for the triangle so those will go the next testcase
Thanks for the hint. My solution was accepted after I removed the break statement.
Reason: If we use break, it prints "Triangle" but after that it breaks. So instead of getting the "n" it just inputs the next line as "n".
For example, in the below case, it should go and input next "n" and not next line i.e. it inputs n = 1110 instead of n = 2
4 0000 0000 0100 1110 2 11 11
For Problem-E, I have written this code but I am not able to understand the problem in my logic , and it gives wrong result for n=10^ and k=10^9. Please help me with this.
include <bits/stdc++.h>
using namespace std;
int main() { int t; cin >> t; while (t) { long long int n, k, m = 1, j = 0; cin >> n >> k; long long int r = 0; long long int x = 0; x = n / 2 + n % 2; while (1) { if (k <= x) { while (k) { r = m * (2 * j + 1); j++; k--; } break; } else { m = m * 2; k = k — x; x = x / 2 + x % 2; } } cout << r << endl; t--; } return 0; }
For problem C, I guess there is a solution with complexity O(t). I am just a beginner, so I can be mistaken. Someone more experienced can check this. Here is my solution :
include
include<math.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
for(int tests=0;tests<t;tests++)
{
int n;
cin>>n;
int e=n%10;
n/=10;
int d=n%10;
n/=10;
int c=n%10;
n/=10;
int b=n%10;
n/=10;
int a=n%10;
n/=10;
int p=n;
bint res=(e+1)*(a+b+c+d+p+e/2)+(d>0)*d*(5*(d-1)+45+10*(p+a+b+c))+(c>0)*c*(50*(c-1)+100*(a+b+p)+900)+(b>0)*b*(500*(b-1)+1000*(a+p)+13500)+(a>0)*a*(5000*(a-1)+10000*p+180000)+(p>0)*p*(50000*(p-1)+2250000);
if(e%2==1)
cout<<res<<endl;
}
return 0;
}
In the solution of Problem G,this option is only need when g[][] is 1.
New to c++ for problem c, can someone please explain why my logic is wrong , couldn't get the subject
why does mysolutionto c shows TLE; i believe i have implemented dp correctly. is bottom up really better that top down approach???
For problem E. What I did is I observed the pattern that it was forming and then I calculated the row in which my no is coming and then i found the position where the no will be and then found out the odd no corresponding to that position and then ans is (oddno*(1<<rowno)),