We hope you enjoyed the contest!
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s, c = "codeforces";
cin >> s;
int ans = 0;
for(int i = 0; i < 10; i++)
{
if(s[i] != c[i])
{
ans++;
}
}
cout << ans << endl;
}
int32_t main(){
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
Idea: mesanu
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int a[n];
int cnt = 0, ans = 0;
for(int i = 0; i < n; i++)
{
cin >> a[i];
if(a[i] == 0)
{
cnt++;
}
else
{
ans = max(ans, cnt);
cnt = 0;
}
}
cout << max(ans, cnt) << endl;
}
int32_t main(){
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
void solve() {
int n; cin >> n;
map<string, int> mp;
mp["00"] = mp["01"] = mp["10"] = mp["11"] = 1e9;
int ans = 1e9;
for(int i = 0; i < n; ++i) {
int x; cin >> x; string s; cin >> s;
mp[s] = min(mp[s], x);
}
if(min(mp["11"], mp["10"] + mp["01"]) > (int)1e6) {
cout << "-1\n";
} else {
cout << min(mp["11"], mp["10"] + mp["01"]) << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
cin >> t;
while(t--) {
solve();
}
}
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
bool ok(int n, int m) {
if (n == m) {return true;}
else if (n % 3 != 0) {return false;}
else {return (ok(n / 3, m) || ok(2 * n / 3, m));}
}
void solve() {
int n, m;
cin >> n >> m;
cout << (ok(n, m) ? "YES" : "NO") << '\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 <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int n, m;
bool vis[1005][1005];
int a[1005][1005];
int dfs(int i, int j)
{
vis[i][j] = true;
int ans = a[i][j];
if(i != 0 && a[i-1][j] != 0 && !vis[i-1][j])
{
ans+=dfs(i-1, j);
}
if(i != n-1 && a[i+1][j] != 0 && !vis[i+1][j])
{
ans+=dfs(i+1, j);
}
if(j != 0 && a[i][j-1] != 0 && !vis[i][j-1])
{
ans+=dfs(i, j-1);
}
if(j != m-1 && a[i][j+1] != 0 && !vis[i][j+1])
{
ans+=dfs(i, j+1);
}
return ans;
}
void solve()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
vis[i][j] = false;
cin >> a[i][j];
}
}
int ans = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(!vis[i][j] && a[i][j] != 0)
{
ans = max(ans, dfs(i, j));
}
}
}
cout << ans << endl;
}
int32_t main(){
startt
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
Idea: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n, m;
cin >> n >> m;
int cnt[n + 1];
for (int i = 1; i <= n; i++) {
cnt[i] = 0;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
cnt[u]++;
cnt[v]++;
}
map<int, int> cnts;
for (int i = 1; i <= n; i++) {
cnts[cnt[i]]++;
}
vector<int> v;
for (auto p : cnts) {
v.push_back(p.second);
}
sort(v.begin(), v.end());
if (v.size() == 3) {
cout << v[1] << ' ' << v[2] / v[1] << '\n';
}
else {
cout << v[0] - 1 << ' ' << v[1] / (v[0] - 1) << '\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: flamestorm
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
long long ans[2000007];
long long a[1500][1500] = {}, curr = 1;
void solve() {
int n;
cin >> n;
cout << ans[n] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (int i = 1; i < 1500; i++) {
for (int j = i - 1; j >= 1; j--) {
a[j][i - j] = a[j - 1][i - j] + a[j][i - j - 1] - a[j - 1][i - j - 1] + curr * curr;
ans[curr] = a[j][i - j];
curr++;
}
}
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
// solve();
}
Idea: SlavicG
Tutorial
Tutorial is loading...
Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int mod = 1e9 + 7;
void solve() {
int n, x; cin >> n >> x;
vector<int> a(n + 1);
vector<vector<int>> dp(n + 1, vector<int>(1 << 6, 0));
for(int i = 1; i <= n; ++i) {
cin >> a[i];
for(int mask = 0; mask < (1 << 6); ++mask) {
dp[i][mask] += dp[i - 1][mask];
if(dp[i][mask] >= mod) dp[i][mask] -= mod;
dp[i][mask & a[i]] += dp[i - 1][mask];
if(dp[i][mask & a[i]] >= mod) dp[i][mask & a[i]] -= mod;
}
dp[i][a[i]] = (dp[i][a[i]] + 1) % mod;
}
int ans = 0;
for(int mask = 0; mask < (1 << 6); ++mask) {
if(__builtin_popcount(mask) == x) {
ans = (ans + dp[n][mask]) % mod;
}
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
cin >> t;
while(t--) {
solve();
}
}
thanks for the speedy editorial.
wow what speedy editorial!
In F , according to the constraints , if m = 1 , then how x and y could be both greater than 1 such that x*(y+1) = m = 1 ?
It is guaranteed that this graph is a snowflake graph.
So m=x+xy>2
m>=1 doesn't means that m can be 1.
In fact, $$$m=n-1$$$
Problem H can be solved in
O(k * 2^k + n)
using AND Convolution, as described here: https://mirror.codeforces.com/blog/entry/115438Implementation: https://mirror.codeforces.com/contest/1829/submission/204840516
Wow, dude, this is really nice! Because of commutativity of AND, this is possible! I thought that I could solve it as well with convolutions, but didn't see the commutativity and ended up solved it using the standard
n * 2^k
dp.This is very interesting! However, I have a major doubt: How did you realize that applying the inverse SoS on the amount of subsets whose AND contain mask is the same as the amount of subsets whose AND is EXACTLY mask? In the linked blog the example is given for pairs, but I fail to realize how to connect these ideas formally. Did you prove it? Can you share your insight?
Maybe I would understand it if I understood why it works for pairs, lol.
Wow. Loved G. Though was not able to solve it. But didn't even notice that just rotating it will make it a standard 2-D prefix sum problem. Lol struggled the whole 1hr during the contest.
Can you please explain it?
I am not able to get the tutorial.
Maybe it's because you don't know that technique called 2-D prefix sum. Google it and study it.
The pattern was [1,3,6..][2,5,9..].
Imagine this as the 2-D grid.
Why I always get tle?
use ios:sync_with_stdio(false); command
Didn't get H
It's a standard dp bitmask problem.
can u refer the classic problem where could i understand it please ?
G is possible by simply using sum of squares of N natural numbers for each row and is easy implementation too.
https://mirror.codeforces.com/contest/1829/submission/204823035
in problem F what is the output of this case :
$$$m$$$ can't be $$$1$$$. (https://mirror.codeforces.com/blog/entry/115896?#comment-1027861)
There is an additional constraint in the inputs.
Tonight's problems' interesting and not typical, thank the writer and codeforces for such a great contest!
Have anyone solved E using BFS?
Yes. 204779800
My guess is that most people solved it using BFS
I solved it with DFS. However, the code was in Go, and I needed a few submissions to optimize memory usage. Strange thing...
Code link : https://mirror.codeforces.com/contest/1829/submission/205309024
please help me,it is giving Runtime error in test-12
If you open the submission details, it says that it's "Stack overflow" error. I don't know Java, but you should either increase the stack size or rewrite it using bfs
I solved by union find.
I did
Alt solution to G:
From the given $$$n$$$, try to go upwards to the left block $$$l_n$$$, and to the right block $$$r_n$$$.
If we can visit both ways, there some blocks will be taken twice. So we find the common blocks between $$$l_n$$$ and $$$r_n$$$ and subtract them from the answer. If the common block exists, it's either $$$l_{r_n}$$$ or $$$r_{l_n}$$$.
There is a recursive relation, until the blocks exist. It can be either of:
Implementation: 204825154
I also coded it this way, but if you look closely, this is actually just the 2d prefix sum solution!
It is!
This is just a different way of looking at it, without all the modifications. I feel this is more intuitive.
this is 2023 per test case right?
originally, this solution was right on the edge of passing :( and was supposed to not pass. But setters remembered its a div4.
It's actually $$$10^6$$$ for all test cases since you can do memoization.
Also you do not need all of the $$$2023$$$ rows.
For problem F, if you see:
Wrong answer on test 2
wrong answer 65th numbers differ
Then it is the
x == y+1
edge case mentioned in editorial.Someone please help me spot the error in my solution to D I spent almost the entire contest trying to fix it:
Here is a failing test case:
Can you see why this happens?
204848665
my solution for (E) The lakes is clearly an O(mn) solution, but was exceeding limit, can anyone explain where it can be optimised?
204858437 is logically same as my solution 204848665 , but the first solution was accepted but mine got TLE, both are written in python
Both are giving TLE now, instead I switched from using input to using readline and it worked.
240657725
Currently you are adding the same squares into the queue multiple times: when adding a new square to the queue you're only checking if you've been to the square before. Importantly, you're not checking if the square has already been added to the queue. This starts actually growing exponentially and the time complexity is definitely not $$$O(nm)$$$. It can be fixed with a small modification — changing the place where some operations are done. This ensures that no square will be added to the queue more than once and the complexity is now truly $$$O(nm)$$$.
204888301
understood, thanks for the explanation
Actually an even simpler modification gets AC:
Before adding the next squares into the queue, just make sure that
grid[i][j] > 0
. Now no exponential growth will happen and each square will get added to the queue at most 4 times.204889157
I tried Solving E using Standard DFS in python Python Code. But I was continuously getting Runtime Error at TC 6. C ++ did the trick though (for the same) C++ code. Can someone tell me why I was getting Error in Python for the same Implementation ?
Python's default stack limit is 1000 which can be changed with
sys.setrecursionlimit(big number)
, on cases where you are forced to recurse deeper then 1000 layers (imagine a spiral pattern of values separated by 0's) you will run time error. (Be careful python recursion is very slow so you may run into issues with time limit)Changing to c++ fixed the issue because c++ does not have this issue with low default stack limit.
Ps Update: Just tried using it. It didn't make any difference in the outcome of the submission (RE Again ;_;). But thanks again, It was Informative.
Since the grid can be of size 1000 * 1000, you would need the stack limit to be that big as well (ie 1e6+ a bit). Though you might lead to TLE since recursion is quite slow in python. I would suggest if you do get tle (and can't switch languages) to try programming the flood fill as an iterative bfs as although the time complexity is the same the constant factor should be significantly faster.
204849479
Why am I getting runtime error in this code?
Python's default stack limit is 1000 which can be changed with
sys.setrecursionlimit(big number)
, on cases where you are forced to recurse deeper then 1000 layers (imagine a spiral pattern of values separated by 0's) you will run time error.wow. so speedy
JUST CAME BACK TO VISIT A TAYLOR SWIFT ROUND. OMG!!
come back...be here (another Taylor's song)
Dear Reader, This is me trying to come back be here at this holy ground. and i almost do, but everything has changed and story of us looks like a tragedy now, so long live, had a marvellous time ruining everything. People in cf are the lucky one and they should stay stay stay.
It's time to go - Yours slayor twift
Can anybody hack this solution for TLE ?? https://mirror.codeforces.com/contest/1829/submission/204874235
Problem F: Observe that the starting vertex is the ONLY vertex that is not a leaf AND also has no leaf neighbours. 204824064
for problem D, why we used this formula for master theorem: T(n) = 2T(n/3) + O(1) , but it's really T(n/3)+T((2*n)/3)+O(1) why we can Ignore that coeff (2*) ? thank you
Usually for time complexity we don't consider coefficients so we just say it is 2 * T(n/3), since the order is the same.
Thank you!
I thik this is wrong. Recurrence T(n)=T(n/3)+T(2n/3) +c has an O(n) solution for T(n). In fact, T(n) is Omega(n). This can be proved by induction easily. That is, you can prove that T(n) >= kn for some k and for every n sufficiently large.
I agree with juanxo_gu the solution using the master theorem given in solution list lower bound of T(n).
Another approach to solve E (https://mirror.codeforces.com/contest/1829/submission/204823633)
Have anybody solved problem E without using bfs and DFS???
Here you can find another approach -> (https://mirror.codeforces.com/contest/1829/submission/204823633)
I just used dsu
how do you solve it using dsu?
$$$H$$$ can be solved in $$$(maxa_i^2 * t + ∑n)$$$ where you can just keep track of how many ways each number between $$$0-63$$$ inclusive can be constructed. Submission: 204886301 (binpower is not needed, we can just pre-calculate powers of $$$2$$$).
There is a much faster solution to D
Each time you either multiply by 1/3 or 2/3, so the final multiplier must be 2^a / 3^b where a <= b.
Then just check whether the ratio m/n can be expressed in that form.
Code (gets AC):
204904968
There is an even more cheesy solution, where once you realize the 2^a / 3^b and a<=b part, you precompute a list of all such fractions of that form within 10^7, and for each test case, just check whether m/n is equal to some fraction in that list:
(also AC)
One thing I noticed on F:
Each node is in the 2nd layer of nodes if and only if its degree is $$$1$$$. That means we can perform the following:
my submission
For E, my dfs solution gives TLE which has the same logic as the solution given. When I changed the visited array and input array to global variables, it got accepted. Why is that so?
because you define a new 2d vector after each test, and that consumes much time
Can someone tell me why my H problem is giving TLE with $$$dp[n][64]$$$ 204872960 but accepted with space optimization 204874966?
Hi, I wonder is this round rated for all who has a rating < 1400? If so, why my ratings didn't change? I'm new to codeforces so please forgive me for asking these questions. Thanks!
System testing is going on, after some time ratings will change.
I solved G in a similar way. Let $$$dp_i$$$ be the answer for $$$i$$$ and $$$x_i$$$ be the row for $$$i$$$. Now we know that $$$dp_1 = 1$$$ , and $$$dp_0 = 0$$$, and for each $$$1 \leq i \leq N$$$ we have two cases:
$$$i - x_i$$$ only lies on the previous row $$$x_i - 1$$$ , then $$$dp_i = i^2 + dp_{i-x_i}$$$
$$$i - x_i + 1$$$ only lies on the previous row $$$x_i - 1$$$ , then $$$dp_i = i^2 + dp_{i-x_i+1}$$$ both $$$i - x_i$$$ and $$$i - x_i + 1$$$ lie on the previous row $$$x_i - 1$$$. Here we can't simply do $$$dp_i = i^2 + dp_{i-x_i} + dp_{i-x_i+1}$$$ because $$$dp_{i-x_i}$$$ and $$$dp_{i-x_i+1}$$$ have common numbers that were added to both of them. We can see that they have different answers except the one they took from $$$dp_{i-2(x+1)}$$$ so it becomes $$$dp_i = i^2 + dp_{i-x_i} + dp_{i-x_i+1} - dp_{i-2(x+1)}$$$
alt for D, we can just check wether we can get from m to n if we can do the operations below
m := 3*m
m := 3*m/2 (if m mod 2 = 0)
So we need to check if there exist integer pair x and y (x >= y and m mod 2^y = 0) s.t. m * 3^x / 2^y = n
we can do it in O(log2(n))
How could H be solved with larger values? for example (a[i] <= 1e6 or 1e9) and of course a corresponding K.
I am Expert now thanks to this round, SpeedForces forever xD
That's a huge jump orz
I dont know why my submission 204795848 in the contest using C++ 17 giving the TLE but it's Accepted if I submit it with C++20 204978167
/* this question is basically of precomputation + dp in this question we will first precompute the matrix then make a loop from 1 to 1e6 and calculate the answer for all the elements and then for each query we will give answer in O(1) / / dp of current element will be the x^2 (dp[k]=(vec[i][j]*vec[i][j])) + dp of the two elements above it if exists i.e. { if(i-1>=0 && j-1>=0) dp[k]+=dp[vec[i-1][j-1]]; if(i-1>=0 && j<vec[i-1].size()) dp[k]+=dp[vec[i-1][j]]; }
*/ // do a dry run you will get by yourself
/* <--------------------------------- THANK YOU -----------------------------> */
https://mirror.codeforces.com/contest/1829/submission/205005182
Just an implementation detail about the tutorial solution for problem 1829H - Don't Blame Me: if you are using GNU C++20 compiler, then you do not have to use the built-in function
__builtin_popcount
. You can use the standard library function std::popcount instead. Also, as the next-state at any index $$$i$$$ depends only on the previous state at index $$$i-1$$$, it is sufficient to compute the answer using two vectors only instead of using a matrix of $$$n+1$$$ vectors.Nice problems ,i have got +156 points (:
Hello! Does anyone know how to solve H using combinatorics?
Can someone post DP solution of G?
See the comment above.
Nice tutoriel
Editor seems to be fan of Taylor Swift
I used topological sorting in F and passed :)
wow!强大!
I used dfs to solve the G, just search the (i — 1, j) and (i — 1, j — 1)!
I am trying to do the same thing , I am getting TLE
In problem G. How do you come up with the number of rows and columns as 1500. Isn't is supposed to be 2024?
summation of first 1500 natural numbers is greater than 1e6.
I think flamestorm is huge fan of taylor swift..
Can someone help me make my code pass? I did problem H with recursive python DP and I'm getting TLE even after memoization and bootstrapping Please Help me get an AC
could someone enlighten me on my TLE for E? thank you in advance :)
update : the issue seems to be the out of bounds check, although im not sure why
author is a huge ts fan i guess :)
In editorial's solution of 1829H - Don't Blame Me, Why is there a need to add this transition
dp[i][a[i]]=dp[i][a[i]]+1
Shouldnt this be counted in the two previous transitions.
Someone please explain
There's another approach for problem F with same complexity but with no constant factors , exactly $$$\mathcal{O}(n+m)$$$
258238660
Can somebody explain why am I getting TLE, even though my time complexity should not be that bad since I am only traversing each number at most once.
submission : 266642608