The problem names are based on my favorite characters out there. Yes, 1554E - You are my most favorite character UwU.
I have tried to make the editorials as interactive as possible. Enjoy.
Tutorial is loading...
Code(C++)
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n);
for (auto &x: a) {
cin >> x;
}
long long ans = 0;
for (int i = 1; i < n; i++) {
ans = max(ans, 1LL * a[i] * a[i - 1]);
}
cout << ans << '\n';
}
return 0;
}
Code(Python)
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
ans = max(ans, a[i] * a[i + 1])
print(ans)
Tutorial is loading...
Code(C++)
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n, k; cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long ans = -1e12;
int l = max(1, n - 2 * k);
for (int i = l; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
ans = max(ans, 1LL * i * j - 1LL * k * (a[i] | a[j]));
}
}
cout << ans << '\n';
}
return 0;
}
Code(Python)
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = max(0, n - 2 * k - 1)
ans = -1e12
for i in range(l, n):
for j in range(i + 1, n):
ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j]))
print(ans)
Tutorial is loading...
Code(C++)
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n, m; cin >> n >> m;
++m;
int ans = 0;
for (int k = 30; k >= 0 and n < m; k--) {
if ((n >> k & 1) == (m >> k & 1)) continue;
if (m >> k & 1) ans |= 1 << k, n |= 1 << k;
}
cout << ans << '\n';
}
return 0;
}
Code(Python)
import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
m += 1
ans = 0
for k in range(30, -1, -1):
if (n >= m): break
if ((n >> k & 1) == (m >> k & 1)): continue
if (m >> k & 1):
ans |= 1 << k
n |= 1 << k
print(ans)
Tutorial is loading...
Code(C++)
#include<bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
if (n == 1) {
cout << "a\n";
}
else {
cout << string(n / 2, 'a') + (n & 1 ? "bc" : "b") + string(n / 2 - 1, 'a') << '\n';
}
}
return 0;
}
Code(Python)
t = int(input())
for _ in range(t):
n = int(input())
if (n == 1):
print('a')
else:
s = 'a' * (n // 2)
if (n & 1):
s += 'bc'
else:
s += 'b'
s += 'a' * (n // 2 - 1)
print(s)
If you are wondering(as you always do 。^‿^。) about the checker:
Checker
Write a suffix automata and check if every node occurs an odd number of times.
Code
#include<bits/stdc++.h>
#include "testlib.h"
using namespace std;
// len -> largest string length of the corresponding endpos-equivalent class
// link -> longest suffix that is another endpos-equivalent class.
// firstpos -> 1 indexed end position of the first occurrence of the largest string of that node
// minlen(v) -> smallest string of node v = len(link(v)) + 1
// terminal nodes -> store the suffixes
struct SuffixAutomaton {
struct node {
int len, link, firstpos;
map<char, int> nxt;
};
int sz, last;
vector<node> t;
vector<int> terminal;
vector<int> dp;
vector<vector<int>> g;
SuffixAutomaton() {}
SuffixAutomaton(int n) {
t.resize(2 * n); terminal.resize(2 * n, 0);
dp.resize(2 * n, -1); sz = 1; last = 0;
g.resize(2 * n);
t[0].len = 0; t[0].link = -1; t[0].firstpos = 0;
}
void extend(char c) {
int p = last;
int cur = sz++;
t[cur].len = t[last].len + 1;
t[cur].firstpos = t[cur].len;
p = last;
while (p != -1 && !t[p].nxt.count(c)) {
t[p].nxt[c] = cur;
p = t[p].link;
}
if (p == -1) t[cur].link = 0;
else {
int q = t[p].nxt[c];
if (t[p].len + 1 == t[q].len) t[cur].link = q;
else {
int clone = sz++;
t[clone] = t[q];
t[clone].len = t[p].len + 1;
while (p != -1 && t[p].nxt[c] == q) {
t[p].nxt[c] = clone;
p = t[p].link;
}
t[q].link = t[cur].link = clone;
}
}
last = cur;
}
void build_tree() {
for (int i = 1; i < sz; i++) g[t[i].link].push_back(i);
}
void build(string &s) {
for (auto x: s) {
extend(x);
terminal[last] = 1;
}
build_tree();
}
int cnt(int i) { // number of times i-th node occurs in the string
if (dp[i] != -1) return dp[i];
int ret = terminal[i];
for (auto &x: g[i]) ret += cnt(x);
return dp[i] = ret;
}
};
pair<int, int> ok(string s) {
int n = s.size();
SuffixAutomaton sa(n);
sa.build(s);
for (int i = 1; i < sa.sz; i++) {
if (sa.cnt(i) % 2 == 0) {
return {sa.t[i].firstpos - sa.t[i].len, sa.t[i].firstpos - 1};
}
}
return {-1, -1};
}
int main(int argc, char* argv[]) {
registerTestlibCmd(argc, argv);
int t = inf.readInt();
inf.readEoln();
for (int test = 1; test <= t; test++) {
setTestCase(test);
int n = inf.readInt();
inf.readEoln();
string s = ouf.readToken();
if (s.size() != n) {
quitf(_wa, "the length of s should be exactly %d", n);
}
for (int i = 0; i < n; i++) {
if (!(s[i] >= 'a' and s[i] <= 'z')) {
quitf(_wa, "s contains %c which is not an English lowercase character", s[i]);
}
}
auto p = ok(s);
if (p.first != -1) {
quitf(_wa, "the substring s[%d, %d] (0-indexed) occurs even number of times in s :\"(", p.first, p.second);
}
}
quitf(_ok, "you are the best problem solver ever UwU");
return 0;
}
Tutorial is loading...
Code(C++)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9, mod = 998244353;
vector<int> g[N];
int dp[N], d, ok, ans[N];
void dfs(int u, int p = 0) {
if (!ok) return;
for (auto v: g[u]) {
if (v ^ p) {
dfs(v, u);
}
}
if (dp[u] % d != 0) {
if (p) {
dp[u]++;
}
if (dp[u] % d != 0) {
ok = 0;
return;
}
}
else {
dp[p]++;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
ans[i] = 0;
}
ans[1] = 1;
for (int k = 1; k <= n - 1; k++) {
ans[1] = (ans[1] + ans[1]) % mod;
}
for (d = 2; d <= n - 1; d++) {
if ((n - 1) % d == 0) {
ok = 1;
dfs(1);
ans[d] = ok;
for (int i = 0; i <= n; i++) {
dp[i] = 0;
}
}
}
for (int i = n; i >= 1; i--) {
for (int j = i + i; j <= n; j += i) {
ans[i] = (ans[i] - ans[j] + mod) % mod;
}
}
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << ans[i];
}
cout << '\n';
for (int i = 1; i <= n; i++) {
g[i].clear();
}
}
return 0;
}
Nice contest :)
I laughed so hard when saw that B's statements were updated and the only update was replacement of 2 with a 1 at the end ;))
A < C < D < B < E
A < B < C < D < E
:|
E>D>C>B>A
A<C<B<D<E
A < D < C < B < E
Such thinkforces B
It was a Bit difficult.
Kudos from India man. I really liked your problems. Looking forward to more Bangladeshi rounds.
Nice contest, solving every problem costs a lot of thinking and observation!
Like these problems, but don't like the order of the problems.
It appears a lot of people misread E (I thought the order of operations mattered, and the total count would be n!). Did none of the testers get this confusion? I feel like different sequences should have been in bold.
Thanks for the nice problems and the quick editorial!
The contest was a BIT difficult
pun intended?
I was referring to problems B and C being about bit operations
binary indexed tree
op
Observation forces
No doubt problems were very good.
Problems were very cool this contest!
The gap between D and E is a bit large.
what about A and B
Rather not say.
Nice contest, I'm fool.
Read the solution for B and wonder where is my brain ::.
harry potter cries in WA, LogicLessNess and FST's
Really liked the problems. Thanks!
Very Nice Contest!
I paused and thought so much to run out of time :P!
I cannot believe this solution passed system test 124170859 on problem D.
Similar Solution: 124179999
I don't think, it can be hacked, although I don't have a proof of upper-bound on no. of characters used is less than 27. (Worst case calculations give upperbound as 3*log3(n))
I hacked both
Wow, You hacked it.
Thank God, you were not in my room.
Fast Editorial!
It's a great contest,I love it.
I thought the contest would exercise my mind, but E was too difficult for me ...... :(
Loved the problemset. But D was easier than both B and C. My solution: 124163494
i.m.o., the problem ordering should've been: A — D — B — C — E
My solution to B is not dependent on k. We have all the value of array less than or equal to n. With this can what can iterate x from 1 to ceil(log2(n)) and assume x= (Ai|Aj). For each x we will find top 2 index with greatest value such the Ai is a sub mask of x.
124158918
How do we know if x exists as OR of ai , aj? And you're not checking every sub-masks?
Could you please explain?
"How do we know if x exists as OR of ai , aj?"
you are correct. It will not exist for every x. But we have to maximize the answer. It may happen that we may end up calculating the candidate answer for certain x which does not exit. But since, we are going through every mask, we will calculate the answer for all submasks of x too. If that submask is actually possible for some (ai|aj) then it will override the answer as submask(x) < x.
"And you're not checking every sub-masks?"
I am checking every submask. The top 2 value I am picking are the maximum 2(if possible) index from all possible submasks. Let say mask y=101010. Then I have already calculated the top 2 value for all the value <y. When I process y I just check these masks (001010,100010,101000), Since these invidually conatins the best 2 indices of all their submask, I am able to get all best two from all submask of y. Also the mask =001000 will contribute to (001010,101000) therefore we may end up having a duplicate index. We have to take care of that.
Thanks a lot, that helped.
Can't understand how binary search will apply for problem c
Help apricated
Why do you need binary search for C??
I have seen some solution with binary search
just curious to know but unable to understand how binary search property makes sense
I did it using Binary search. So just try to understand the monotonic behaviour.
Let Count(n,m, mid) is the function, that can give you the count of numbers that are less than or equal to mid from set of [n^0, n^1, n^2 ... n^m]; if(count(n,m, mid)==mid+1) it simply means our mex can't be less than or equal to mid+1.
Now the only thing is how to build count() optimally. so it's a kinda digit dp problem. for more insights. u can visit my solution. https://mirror.codeforces.com/contest/1554/submission/124189939
Can anyone please explain B in some easy way??
Well, my solution is different from the one in the editorial (and I don’t really think it’s simple), but here it is:
If you fix the Ai | Aj part (let’s call it x from now on), you have to find 2 biggest indices i and j such that Ai | Aj = x, and then the solution is i * j / (k * x). Notice that 0 <= x <= 2 * n because Ai <= n. So, you can go through all x’s, and for every x, find the 2 biggest indices such that the numbers (bitmasks) on those indices are subsets of x (you can just go through all the subsets, check if they appear in the array and modify the max indices accordingly). But, the complexity of that is about n^2 if you’re not careful with implementation, so you have to use an optimization from this blog (look at the Suboptimal solution part, that optimization reduces the time complexity to 3^(log2(n))). And, I know I’m terrible at explaining things, so I suggest you to take a look at my code.
Also, did anyone else have a similar approach? All the codes I’ve seen have an approach like the one in the editorial.
Yes, I solved it using SOS DP in O(NlogN) but still got TLE in system testing. And the same code was accepted in CPP 11 after the contest. :(
https://mirror.codeforces.com/blog/entry/93321?#comment-822763
can u explain that we can be sure if x would present as Or of ai, aj. That is not any two subsets of x will give x on Bitwise Or.
Yeah, that's true, but if we go through all possible x's, we'll definitely find the optimal solution, because, if some x is not present as an Or of Ai and Aj, then it's definitely not the best one (for some two numbers, their Or is the smallest number which contains them both as subsets, so all bigger numbers can be ignored). So, those x's won't affect the final solution.
It is actually better if max two subsets of x will not give x on bitwise or, as their bitwise or guaranteed to not exceed x.
Max value of $$$k(a_i\vert a_j)$$$ is $$$2kn$$$. So if you take a potential answer and see it is possible to increase $$$ij$$$ by more than $$$2kn$$$ you are guaranteed to arrive at a better answer. This rules out a significant portion of potential answers and you can brute force the rest in $$$\mathcal{O}(kn)$$$.
Any meaning to the problem names?
for B I only checked all pairs for last K + 2 elements, instead of last 2K elements and still didn't FST. thanks
Yeah, we can do it too. But we don't need to be precise, all we want is a good upper limit and AC.
Hello I'm new to codeforces and also am a beginner I wrote this code for cherry problem but only pretests(2) are passed can someone explain me the reason?
In the contest time, only two test cases will be judged and after the contest, other test cases will be judged that's why you can see pretests(2) passed.
ok thanks bro
Hi akhilphenom
provide code in spoiler so it takes lesser space.
Its better to leave now, than later.
Pause_and_thinkforces
I had a hunch even before the contest that D was going to be relatively easy (didn't expect it to be this easy though). Only if I had believed in my instinct! Excellent (and a bit unusual) problemset.
B can be easily done with sum over submask SOS Dp
see my sol
Hey, can someone prove why this approach doesn't get TLEd? I'm not able to figure out.
Iterate on all the numbers in the array. Let's denote a number $$$a[i]$$$ special if there is no number $$$a[j]$$$ such that $$$j\gt i$$$ and $$$a[j]$$$ is a submask of $$$a[i]$$$. When reach and index $$$i$$$ in the array and $$$a[i]$$$ is special, we run a brute force for $$$j\lt i$$$. But we terminate the brute force loop when we find a number $$$a[j]$$$ such that $$$a[j]$$$ is a submask of $$$a[i]$$$ (but we do calculate $$$f(i,j)$$$ for this $$$j$$$).
Solution.
Or if someone could hack this solution :3.
It is one of the best editorial because when i read every line i can connect with the thought process of the author and it is also beginner friendly to understand the concept. Thank You YouKn0wWho.
My solution to Problem D which requires O(log(n)) different characters, which, unfortunately, does not enter(
Suppose we have a matching string s. Then the string s + x + s + y + s is also suitable, where x and y are symbols that have not been encountered before. After all, each substring in s occurs 3 * the past number, and a substring containing symbols x or y occurs 1 time. Also from s you can make a string from |s| + 1 characters just by adding x to the end. Thus, you can make an algorithm similar to fast exponentiation: Let gen (n) return a good string of n characters.
if n % 3 == 2 gen(n) = t + x + t + y + t, t = gen(n / 3) else gen(n) = gen(n — 1) + x
It can be seen that every 3 steps n will decrease at least 3 times, but this will require 4 new symbols. Therefore, you will need log_3(n) * 4 = 44 characters
P.S. I don't know English well and all the text was translated by google translate
I wanted to elaborate on Your idea. Actually, I managed to pass the tests using recursive approach during contest 124173130. As You mentioned it would be convenient to think of the problem recursively. We want to make three copies (just two will not do) of the same string and then and then separate them, by some characters that not be use later in recursion. Each of those recursive string should also maintain task requirements. It is easy to see that if some substring of final solution is maintained entirely inside of one of these substrings then there are three copies of it.
But we also have to be careful when it comes to substrings that are not entirely obtained in one of recursive areas. We aim for our separators to make them unique (or also have three copies).
There three recursive cases. Let's $$$N$$$ be length of recursive call, and $$$A$$$ be the lowest character we are still able to use. Also lets name by $$$B$$$ second lowest character. We will show that it suffices only to use two characters for separation and then $$$2 \cdot \log_3{N} \le 26$$$ (which will allow us not to run out of letters)
First case is $$$N \% 3 = 2$$$. This case is easy. If we mark recursive string of length $$$(N-2)/3$$$ by $$$R$$$, then we can create solution as follows:
$$$S = R + A + R + B + R$$$
Second case is a bit more tricky. We have $$$N\%3=0$$$. We are still able to create separators, but they will be lightly longer (also length of $$$R$$$ will now be $$$(N-6)/3$$$).
$$$S = R + ABAA + R + BB + R$$$
Last case was the most tricky one ($$$N\%3=1$$$). I used the following construction.
$$$S = R + BA + R + B + R + B$$$
Now we added something after the last recursive part, but it was necessary if we want to have total length of separators equal to 4 (7 is impossible, because one of the letters would be used even number of times, and 10 is quite long and I was not able to find any two separators that would solve this problem, and also we are not careful may run out of digits for base cases).
The last thing is just to look at base cases of the recursion, but we can then use consecutive letters that are left (and there is at least 6 of them).
I was so upset because of my bad contest day but the first line melted my heart -
As for me it isn't adequate to have at contest problems with rating: 800-1700-1800-1800-!2600!
Also I think, that order should be like A-D-B-C-E, or smth similar
But I really like each task separately, thnk u!
E can be solved in $$$O(n \log^2 n)$$$ time if the product $$$\prod_{i=1}^{k} (a_i + b_i x)$$$ can be calculated in $$$O(k \log k)$$$ time.
Basically, if we let $$$f(k)$$$ be the ways to construct $$$a$$$ such that gcd is $$$k$$$, then then the mobius transform of $$$f$$$ can be calculated using dp (and fast polynomial products to calculate the dp values). After that, we can take the inversion.
I submitted the simple dnc code to calculate the product, which adds another log, and it passed.
Anyway, I thought the easter egg in E's naming was You = u = $$$\mu$$$. Seems like the intended one is different :P
If $$$a_i \leq n$$$ and $$$a_j \leq n$$$, then $$$(a_i|a_j) \leq 2n$$$.
Can someone, give me an example when $$$(a_i|a_j)$$$ is equal to $$$2n$$$.
It can be shown that this inequality is strict.
Multypling by 2 is bit shift to the left by one, adding zero, so if most significant bit of $$$n$$$ is $$$i$$$-th bit, for $$$2n$$$ it is $$$i + 1$$$-th bit, but if $$$x \leqslant n$$$, $$$x$$$ can't have $$$i + 1$$$-th bit.
But there's an example where $$$x \leqslant n, y \leqslant n$$$ and $$$x \mid y = (2n - 1)$$$.
Consider $$$n = 2^k$$$, $$$x = 2^k$$$, $$$y = 2^k - 1$$$. $$$x \mid y$$$ is equal $$$2^{k + 1} - 1 = 2n - 1$$$
Yes, but quoting the editorial of B
$$$a_i|a_j = 2n$$$ is not possible.
So shouldn't the symbol be $$$<$$$ instead of $$$\leq$$$ , in the last part ?
consider $$$2n=00...001....$$$, where first 1-bit is k-th bit.
now let's look at $$$n$$$. $$$k$$$-th bit will be set to zero, because dividing by 2 is equal to right-shifting by one.
one of $$$a_i$$$ or $$$a_j$$$ must have k-th bit set to 1 (because $$$2n$$$ has). but that means than one of them are greater than n, because $$$2^k > 2^{k-1} + 2^{k-2} + ...$$$. this is impossible, so $$$a_i|a_j$$$ cant be equal to 2n.
you are right
Has anyone tried to solve B using SOS DP and got TLE in system testing, in C++ 17?
In the editorial it is mentioned that O(N*K) is allowed, so O(NlogN) shouldn't give TLE I guess, I mean K can be as large as 100 and logN cannot be more than 20.
What's more traumatic? The same code was accepted in C++ 11 after the contest. That too in 530 ms. Difference of few ms is fine but 470 ms is too much, if this is the case then there should be different time limits for C++17 and C++11, even 1.2 sec would have worked. :(
It would be great if someone can explain the reason behind this difference in time, and also if anyone can justify why O(N*K) works but O(NlogN) gives TLE. (Except advising for compiler optimizations, if it was so necessary then it should be mentioned in the announcements or in the problem statement, or at least a pretest should be there which fails without these optimizations. All I want to say is if O(NlogN) fails without these optimizations then O(N*K) shouldn't be accepted too. <;_;> )
Here are the submission links:
C++ 17 (TLE)
C++ 11 (Accepted)
Can you tell why changing the order of for loop is giving wrong answer? like this — rep(mask,0,N) rep(i,0,lg)
Can someone explain why k * (ai | aj) is O(100 n)? Wouldn't it still be n^2 because to compute (ai|aj) you need to cycle through every j for each i still
I also had this confusion when I read it. It's not about time complexities, I don't know why they used $$$O$$$ notation. It's just talking about the order of the values. $$$i \cdot j$$$ could potentially go up to $$$n \cdot (n-1)$$$, while $$$k \cdot (a_i | a_j)$$$ can only go up to $$$100 \cdot (2n-1)$$$, which shows that $$$i \cdot j $$$ will be way bigger than $$$k \cdot (a_i | a_j)$$$ because of the constraint on $$$k$$$.
deleted
Great prombles! ^ ^
The problems are well-prepared, but I really wonder why the D is a constructive problem without any other algorithms. In my opinion, this one should be used as B(so I think swapping B and D is a good choice).
Although I know no one will vote for my comment, but I still want to say the suggestions as the writer may hold a round again in the near future.
interesting E ...... (but too difficult TAT)
Very short and nice statements. But when i see "mikasa", i expected some back story about author and this girl :)
hurt_FOR_heart Did you called me?
i think the B is wonderful !!
nb
In problem B, Code(C++), for the case n=6,k=1,a={0,0,0,0,100,100}, it returns -70. Isn't it exactly 12? but i forgot that a<=n. Sorry.
why we shoule do this (if (m >> k & 1) ans |= 1 << k, n |= 1 << k;)operation at Problem C?
Editorial for ABCD be like
I think the solution of D is so wonderful , because I have a complex and strange solution which I have to proof it doesn't exceed 26 distinct character.
YouKn0wWho
There is a typo in the editorial for C:
If $$$n_i = p_i$$$ then we can set $$$k_i = 0$$$ as $$$n_i \oplus 0 = n_i <= p_i$$$.
At the very end it should be $$$n_i >= p_i$$$.
thanks, updated.
problem B: Cobb , are you mean domnick cobb?
Yes
Can someone have a look at my code for problem B? The runtime should also be O(n-k), but it timed out on test case 2. It really doesn't make sense... The intuition is that f(i,j)>f(n,n-1) is only possible when i,j >= sqrt(f(n,n-1)), which is similar to what is given in the tutorial
Thanks!!!
int64_t ans = n * (n - 1) - k * (nums[n - 1] | nums[n - 2]);
You're running into an overflow here. Even though
ans
is a 64-bit int, n is 32-bit, and when computingn * (n - 1)
the value is stored temporarily in a 32-bit int which causes an overflow.Try typecasting any values to 64-bit integers when you run the risk of overflows, like so -
int64_t ans = (int64_t)n * (n - 1) - (int64_t)k * (nums[n - 1] | nums[n - 2]);
Also, you should ideally either link to your code, or enclose your code in
You can use spoiler tags by typing < spoiler summary="Whatever" > -your-text- < /spoiler > (without the spaces between angled brackets and tags)
.
Is there an approximate value of $$$σ_0$$$ represented by Big O notation less than $$$O(\sqrt{n})$$$?
$$$\mathcal{O}{(\sqrt[3]{n})}$$$
It is not proven fact(maybe it is wrong), but for all numbers less than $$$10^{12}$$$ it is barely true. But you shouldn't use this in asymptotics.
B should have been a little easier or A should have been a little more difficult!
Hello, (in problem B) could someone explain why my this submission didn't pass while this similar solution does. Any effort will be appreciated.
Update: There was an integer overflow and it got accepted after fixing it. Accepted solution after fixing overflow. (Thanks)
An excellent contest which improve my understanding of the BIT problem, but the only pity is that I didn't attempt to have a look at the D I should have solved ,'casue I failed to solve the B and C.
i still not able to understand B(1554B — Cobb) Can anyone please explain B in some easier way
Ok so I overkilled B. But I have a very different solution for B. I used SOS dp for B, where the constraints on k does not matter at all. Just store for each mask the two highest indexes of its all submasks which can be easily calculated using SOS dp. Then just choose the maximum value among all masks.
I can't understand the calculation formula of Hk of problem E. Can someone explain it?
Problem E — hk calculation :
fk = number of sequences divisble by k => this will include all sequences that are also divisble by 2k, 3k, 4k, 5k and so on;
So, to get the number of sequences where the gcd is k, you can subtract sequences where gcd is greater than k and which were counted in fk
So h(k) = f(k) — h(2k) — h(3k) — h(4k) ......h(ik) ---- until ik becomes > n;
h(k) needs to be calculated from higher to lower values ofc.
Hope this helps you
Well, my solution for B, is for every 1 <= j <= n, search every i in max(1, j-k) <= i < j, and try to use f(i, j) to update the answer. I know it isn't a correct solution, but I passed the system test...
About Problem E,there is a similar solution with better Time Complexity.
Noticed that for any $$$k>1$$$,a sequence $$$a$$$ such that each $$$a_i$$$ is divisible by $$$k$$$,it can be proved that there will be at most one condition.
Then we can only check $$$x|n-1$$$ and $$$x$$$ is prime.And when we check whether a sequence $$$a$$$ such that each $$$a_i$$$ is divisible by $$$x$$$ can be build,we could find the exact gcd number at the same time.
It runs much faster than the code of Editorial.
The exact time complexity should be $$$\mathcal O(n\omega(n-1))$$$ while $$$\omega(x)$$$ is the number of prime number that can divide $$$x$$$.I think it is no more than $$$\mathcal O(n \log \log n)$$$
This is my code.
When $$$n - 1$$$ is a primorial, $$$\omega(n - 1) \sim \frac{\log n}{\log \log n}$$$.
D is too overrated imao, but I'm glad that I read ALL PROBLEMS and quickly solved D. Wonderful contest!
Just Awesome
excuse me ,what does the problem E the last step h_k = f_k — \xi h_ik mean
Try to solve problem D if we consider subsequence instead of substring! It is an interesting problem.
Can someone explain problem E why two dfs with different roots can't form two different A sequences?
can anyone tell why i am getting tle here in D void solve() { ll n; cin>>n; ll i,k; string ans=""; if(n%2==0) { if(n==2) { cout<<"ab"<<endl; } else { k=(n-1)/2; for(i=0;i<=k;i++) { ans=ans+"a"; } ans=ans+"b"; for(i=0;i<k;i++) { ans=ans+"a"; } cout<<ans<<endl; } } else { if(n==1) { cout<<"a"<<endl; } else { k=(n-2)/2; for(i=0;i<=k;i++) { ans=ans+"a"; } ans=ans+"bc"; for(i=0;i<k;i++) { ans=ans+"a"; } cout<<ans<<endl; } } }
Time complexity of string addition operator is generally linear in the resulting string length. https://www.cplusplus.com/reference/string/string/operator+/
So if you have these lines of code:
That block of code is actually O(n^2)
Thi is My approach For Problem C
we know the xor properties 0 xor 0 = 1 xor 1 = 0 1 xor 0 = 0 xor 1 = 1 so in this ques we have concluded the condition n^MEX>=(m+1)
we have to find MEX(as small as possible) so we will move from 31 st bit to 0th if we found ith bit of n and ith bit of (m+1) is same then will make ith bit of MEX is 0(keeping in mind that we r making MEX as small as possible)and if ith bit MEX is 0 and (m+1)'s ith bit is 1 then we will make ith of bit of MEX is 1 (if we take zero it becomes smaller than m+1) and finally if ith bit MEX is 1 and (m+1)'s ith bit is 0 then we can say thay MEX is already greater then will add zeroes in further position !
**MY CODE : **124203362
why don't we start from 0-th bit instead of 30-th bit,since we want to find smallest k. starting from 0-th bit we can proceed as->for i-th bit from right we add 1<<i to temp(variable) and using another loop we add to temp every (1<<j) [∀j<i:n&(1<<j)==0].
Beacuse n^mex >= m+1, so starting from zero might not result in this condition
The Python code for B in this editorial (YOUR OWN CODE) is giving TLE on test case 10. https://mirror.codeforces.com/contest/1554/submission/124273841
This difference language should not be there..... if the algorithm implemented is same.
How in 2nd statement of problem B maximum possible of value any ai|aj is ≤2n
as ai<=n
so ai=aj=n;
n|n==n
but how n|n==2n ??
Can anyone explain the logic behind problem E observation 2 ?? Why will there be only 1 or no sequence for k>1??
In observation 1 they describe an algorithm starting with: "Let's construct a sequence $$$a$$$ such that each $$$a_i$$$ is divisible by $$$k$$$."
The idea is that by looking at the algorithm's steps, everything is essentially fixed. There's no place in the algorithm where you can choose from a variety of options. You can either complete the algorithm or you can't. If you can complete the algorithm for some k, then f(k) is 1. And if you can't, f(k) is 0.
I would recommend you to take a couple of sample trees and trying out the algorithm by hand for constructing the sequence $$$a$$$ for a fixed $$$k$$$.
E can be solved in $$$O(n \log \omega(n - 1))$$$ where $$$\omega(x)$$$ is the number of distinct prime divisors of $$$x$$$. There is a conclusion that $$$\omega(x) = O(\frac{\log x}{\log \log x})$$$, so the complexity can be written as $$$O(n \log \log n)$$$.
Code
Can someone explain the 2nd observation in the Editorial of last question?? " f(k) for k>1 is either 0 or 1."
In observation 1 they describe an algorithm starting with: "Let's construct a sequence $$$a$$$ such that each $$$a_i$$$ is divisible by $$$k$$$."
The idea is that by looking at the algorithm's steps, everything is essentially fixed. There's no place in the algorithm where you can choose from a variety of options. You can either complete the algorithm or you can't. If you can complete the algorithm for some $$$k$$$, then $$$f(k)$$$ is 1. And if you can't, $$$f(k)$$$ is 0.
I would recommend you to take a couple of sample trees and trying out the algorithm by hand for constructing the sequence $$$a$$$ for a fixed $$$k$$$.
Can somebody explain why in problem E the formula of $$$h_k$$$ is $$$h_k = f_k - \sum\limits_{i=2}^{\left\lfloor\frac{n}{k}\right\rfloor} h_{i\cdot k}$$$, but not for example $$$h_k = f_k - (h_{2k} + h_{3k} + h_{5k} - h_{6k} + \ldots)$$$ (like the inclusion-exclusion formula)?
UPD: Ok, I understood. That's because $$$h_k$$$ is the number of sequences that have gcd equal to $$$k$$$ (not gcd divisible by $$$k$$$).
Problem E is very nice! Masterpiece of small observations!
Awesome from problems to the turorial
Not sure if this is too obvious or has already been covered, but you don't really need dp. For each $$$k \gt 1$$$ you simply need to figure out if the tree can be split into stars with number of edges divisible by $$$k$$$. Instead of doing dp yourself, you can show by induction that this problem is equivalent to the following check. For each vertex, consider the sizes of its subtrees. If some are divisible by $$$k$$$, ignore them. For the rest, if there is one which is $$$\gt 1$$$ mod $$$k$$$, it is not possible. Otherwise count those which are 1 mod $$$k$$$, and their number should be divisible by $$$k$$$. This way you need to do only one dfs in the beginning to calculate subtree sizes. It seems more efficient overall...
What is the Binary Search solution for Problem C?
Modified B- Given an array of integers and a value k. Find the maximum value of
Constraint: n<=1e5,a[i]<=1e9,k<=1e9
Yup I have solved the same. keeping the maximum {i,j} saved for every possible OR value and Answering. Loop takes NLogN for iterating over all submasks who can lead to a given OR value.264097496
I couldn't understand the solution for problem C T_T. Is there any reference or other similar problem to problem C ?