Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n;
string s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
cout << n + 1 << endl;
}
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n;
vector <int> a;
bool solve(){
cin >> n;
a.resize(n);
for(auto &i : a) cin >> i;
ll sum = 0;
for(int i = 0 ; i < n ; i++){
sum += a[i];
if(sum <= 0) return 0;
}
sum = 0;
for(int i = n - 1 ; i >= 0 ; i--){
sum += a[i];
if(sum <= 0) return 0;
}
return 1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
if(solve()) cout << "YES\n";
else cout << "NO\n";
}
}
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
ll x;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> x;
vector <ll> f;
for(ll i = 2 ; i * i <= x ; i++){
if(x % i == 0){
ll cur = 1;
while(x % i == 0){
x /= i;
cur *= i;
}
f.push_back(cur);
}
}
if(x > 1) f.push_back(x);
int n = f.size();
ll ansa = 1e18, ansb = 1e18;
for(int i = 0 ; i < (1 << n) ; i++){
ll a = 1, b = 1;
for(int j = 0 ; j < n ; j++){
if((i >> j) & 1) a *= f[j];
else b *= f[j];
}
if(max(a, b) < max(ansa, ansb)){
ansa = a;
ansb = b;
}
}
cout << ansa << " " << ansb << endl;
}
easier implementation
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
ll x;
ll lcm(ll a, ll b){
return a / __gcd(a, b) * b;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> x;
ll ans;
for(ll i = 1 ; i * i <= x ; i++){
if(x % i == 0 && lcm(i, x / i) == x){
ans = i;
}
}
cout << ans << " " << x / ans << endl;
}
Tutorial is loading...
code
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n;
vector <int> a;
int solve(vector <int> &c, int bit){
if(bit < 0) return 0;
vector <int> l, r;
for(auto &i : c){
if(((i >> bit) & 1) == 0) l.push_back(i);
else r.push_back(i);
}
if(l.size() == 0) return solve(r, bit - 1);
if(r.size() == 0) return solve(l, bit - 1);
return min(solve(l, bit - 1), solve(r, bit - 1)) + (1 << bit);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
a.resize(n);
for(auto &i : a) cin >> i;
cout << solve(a, 30) << endl;
}
Tutorial is loading...
Author: MikeMirzayanov
code (pikmike)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define x first
#define y second
using namespace std;
const int INF = 2e9;
typedef pair<int, int> pt;
map<int, int> ls;
int get(vector<pt> a){
int cnt = 0;
int l = -INF, r = -INF;
sort(a.begin(), a.end());
forn(i, a.size()){
if (a[i].x > r){
if (r != -INF)
ls[l] = 0;
++cnt;
l = a[i].x, r = a[i].y;
}
else{
r = max(r, a[i].y);
}
}
ls[l] = 0;
return cnt;
}
void process(vector<pair<int, pt>> &qr, vector<int> &ans){
set<int> open;
forn(i, qr.size()){
vector<int> op, cl;
int j = i - 1;
while (j + 1 < int(qr.size()) && qr[j + 1].x == qr[i].x){
++j;
if (qr[j].y.x == 1)
op.push_back(qr[j].y.y);
else
cl.push_back(qr[j].y.y);
}
if (open.size() == 1 && !op.empty()){
++ans[*open.begin()];
}
for (auto it : op){
open.insert(it);
}
for (auto it : cl){
open.erase(it);
}
i = j;
}
}
void solve(){
int n;
scanf("%d", &n);
vector<pt> a(n);
forn(i, n) scanf("%d%d", &a[i].x, &a[i].y);
vector<pair<int, pt>> qr;
forn(i, n){
qr.push_back({a[i].x, {1, i}});
qr.push_back({a[i].y, {-1, i}});
}
sort(qr.begin(), qr.end());
vector<int> ans(n, 0);
ls.clear();
int cur = get(a);
process(qr, ans);
forn(i, n) if (ls.count(a[i].x)) ++ls[a[i].x];
forn(i, n) if (ls[a[i].x] == 1) --ans[i];
printf("%d\n", *max_element(ans.begin(), ans.end()) + cur);
}
int main(){
int tc;
scanf("%d", &tc);
forn(i, tc)
solve();
}
Tutorial is loading...
code (mohammedehab2002)
#include <bits/stdc++.h>
using namespace std;
#define MX 100000
int arr[100005],u[MX+5],cnt[MX+5];
vector<int> d[MX+5];
bool b[MX+5];
int coprime(int x)
{
int ret=0;
for (int i:d[x])
ret+=cnt[i]*u[i];
return ret;
}
void update(int x,int a)
{
for (int i:d[x])
cnt[i]+=a;
}
int main()
{
for (int i=1;i<=MX;i++)
{
for (int j=i;j<=MX;j+=i)
d[j].push_back(i);
if (i==1)
u[i]=1;
else if ((i/d[i][1])%d[i][1]==0)
u[i]=0;
else
u[i]=-u[i/d[i][1]];
}
int n;
scanf("%d",&n);
long long ans=0;
for (int i=0;i<n;i++)
{
int a;
scanf("%d",&a);
ans=max(ans,(long long)a);
b[a]=1;
}
for (int g=1;g<=MX;g++)
{
stack<int> s;
for (int i=MX/g;i>0;i--)
{
if (!b[i*g])
continue;
int c=coprime(i);
while (c)
{
if (__gcd(i,s.top())==1)
{
ans=max(ans,1LL*i*s.top()*g);
c--;
}
update(s.top(),-1);
s.pop();
}
update(i,1);
s.push(i);
}
while (!s.empty())
{
update(s.top(),-1);
s.pop();
}
}
printf("%I64d",ans);
}
thanks for the fast editorial :D
Wow editorial even before system testing!!
The problems were beautiful. Can someone please suggest some similar Project Euler questions to the $$$F$$$ ? It definitely feels like a standard question.
P.S. — Here are my solutions to this lovely contest in case anybody wants to refer my code.
https://projecteuler.net/problem=501 https://projecteuler.net/problem=606 https://projecteuler.net/problem=625 https://projecteuler.net/problem=642 https://projecteuler.net/problem=639 https://projecteuler.net/problem=484
https://codeforces.net/contest/547/problem/C
It's the first time I have seen editorial coming out before testing. Thanks :D
https://codeforces.net/contest/1285/submission/68548219
why my solution fails on pretest 7? what is wrong in my appraoch.
You initial
ll ans = LONG_MAX
. If answer more than LONG_MAX you get WA. You can use LLONG_MAXcan some help me with F classical problem
There's an explanation in the editorial. If you have a more specific question, you should ask it. Simply asking people to explain the problem while there's already an explanation for you to read won't get you anywhere.
Is g if Gcd of whole array
g is the possible GCD of the answer. The answer is eventually going to be LCM(x, y)=x*y/GCD(x, y) for some elements x and y in the array. So, we ask the following question: if I know just the GCD of the two elements that have the max LCM, is it easy (computationally) to find out what the max LCM should be? The answer is yes, and so we iterate over all possible values of what the GCD can be.
Are we storing gcd of all possible pair
You should try answering these questions yourself first. What would the time complexity be of storing the GCD of all possible pairs? Well, you would have to iterate over all pairs, which would be $$$O(n^2)$$$, so we're not doing that.
I'll say that F is not an easy problem, and you should have some comfort with number theory and algorithms before tackling it. There are probably easier number theory problems you should tackle first.
F : CLassical? Press X for doubt
Why tutorial is not available for problem E ?
I hope they are preparing a great editorial of problem E.
Unfortunatly they did not.
F accepted with a strange bitset solution 68535522
F accepted with a more strangle bitset solution that I can't even calculate it's complexy :D 68657039 Can you help me calculate the complexy?
Maybe O(能过) (
In fact, I think my solution is $$$O(n^2\log n(\frac{1}{32}+\frac{1}{50}))$$$, where 32 is length of int, and 50 is some magic number. However, it passed.
can please explain what method you have apply
After some hard computing,the complexy that I calculate is O(n^2 log n*log log n/32) After adding some strange optimizations it passed.
It is so hard to calculate it.I use some calculus knowledges to calculate the complexy,but since I'm only 13 and know very little about calculus,maybe I'm wrong.
For problem C: My Submission 68521316, I know I did some extra work still I feel that it should not give TLE. Can someone explain why it TLED on the very last case?
Test case 84: 963761198400
because it has 6720 factors and may perform bad on worse than n^2 TC
Maybe this number has 6720 factors, which is much more than any cases before. $$$6720^2log(N)$$$ could cause TLE.
In fact if you iterate over the grater number in a pair of divisors and break immediately after finding an answer, your solution shall pass. Is is safe to approximate number of divisors of X by the cube root of X?
I wrote this solution without thinking much knowing the n^(1/3) bound as discussed here. But as it failed on the very last test, It's better to find a better solution.
Can you share who is the author of each problem?
ABCD: me
E: MikeMirzayanov
F: TripleM5da
mohammedehab2002 is a solution author.
OK, my guess was wrong.
Strange that nobody stopped you from using C. A was nice though.
What's wrong with C? Actually, in the beginning, we weren't aware of the easy implementation! The intended solution was the first one in the editorial.
What was your guess though? :)
I think I solved that one... 6 years ago? For the first time, I mean. Not sure that today was in first 5.
My guess was that C was added by MikeMirzayanov to "balance the difficulty".
please explain F problem
For D, how does recursively solving for each of the groups ensure that we will reach an optimal answer?
We are iterating over bits from higher to lower bit. Now it is known that 2 ^ i > 2 ^ i — 1 + 2 ^ i — 2 + ... + 1
There can be two cases,
Just handle these cases.
But then how is the complexity of code not 2^n
The recursion depth won't be greater than log(a). Now notice that, when we handle the 2nd case, one might think that we will consider the same set of numbers twice. But it is not, because, these two cases are actually complementary set (Because if a number is in L set, it won't definitely be in R set), so we will end up considering each number at most log(a) times.
how can we solve D using TRIE..plz tell
https://codeforces.net/contest/1285/submission/94495020
delete
1300+ now, how long it will take me to get 1600+?
? what do you mean by saying it?
In problem C how can they say there can be atmost 11 prime?
2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 > 10 ^ 12
Why does the complexity at problem D is O(Nlog(a)) instead of O(N*2^(log(a))) = O(N*a) ?
In the worst case, you will iterate over all numbers of array log(a) times.
Thanks
Problem B, 10, 10 5 -12 7 -10 20 30 -10 50 60
The maximum tastiness for Adel is 110, the sum of the array is 150, why is the answer No?
Nevermind, I found my own mistake.
what was the mistake? I still don't know why the answer is No
Solution without any difference array or binsearch — 68562620
for(int i = 0 ; i < (1 << n) ; i++){ ll a = 1, b = 1; for(int j = 0 ; j < n ; j++){ if((i >> j) & 1) a *= f[j]; else b *= f[j]; } I know they are bitwise operator and left shift and right shift but what it is doing can someone tell
please explain f classical problem
Your F is easy to optimise to $$$\mathcal{O}(V \log V)$$$, where $$$V$$$ is the upper bound on the input values.
For any input number $$$a$$$, we can add its divisors into our input numbers, as using $$$a$$$ over them cannot make the LCA smaller. This is easy to do in $$$\mathcal{O}(V \log V)$$$ time.
Then it suffices to find the two coprime numbers with maximum product, as if the optimal answer uses numbers $$$a$$$ and $$$b$$$, it could just as well use the coprime numbers $$$a$$$ and $$$\frac{b}{gcd(a, b)}$$$ instead. Then we do not need to loop $$$g$$$, and doing what is described in the editorial for $$$g = 1$$$ takes $$$\mathcal{O}(\sum_{i = 1}^{n} \sigma_{0}(i)) = \mathcal{O}(V \log V)$$$ time.
Submission: 68564248
Okay this is so cool! I'll try to compile the other solutions I know here.
There's an $$$O(V^2)$$$ idea: for each number $$$a_i$$$ we can repeat the following: keep a list of all the numbers in the array, and take the largest element in the list; let's call it $$$m$$$. You can maximize the answer with $$$LCM(a_i,m)$$$, and then every multiple of $$$GCD(a_i,m)$$$ is useless because it can never give a better answer, so you can erase them from the list and repeat.
If instead of the list you keep a 0/1 array that tells you which elements exist in the list, you can optimize all these operations with bitset to achieve an $$$O(\frac{V^2}{32})$$$ solution.
Credits: FSTForces
There's also a good randomized solution based on this trick. Choose a random permutation, and then the expected number of indices that increase the answer is $$$O(log(n))$$$. So, for each element in the permutation, if you can check whether it could increase the current answer and the check is positive, you can just try LCMing it with every single element in the array, since there are only $$$O(log(n))$$$ elements that will give a positive check! To perform this check, you can iterate through the divisors $$$d$$$ of $$$a_{p_i}$$$, and then you want to check if there's an element $$$a_j$$$ such that $$$\frac{a_{p_i}*a_j}{d}>ans$$$ and $$$GCD(a_{p_i},a_j)=d$$$. Which is equivalent to $$$a_j>\frac{ans*d}{a_{p_i}}$$$ and $$$\frac{a_j}{d}$$$ is coprime with $$$\frac{a_{p_i}}{d}$$$. The editorial describes how to count the number of elements coprime to an element in an array. This problem is the same but for a suffix.
We also received a ton of heuristics that, together, are probably unbreakable.
any other approach for problem D ??
You can solve it using trie :)
Nice! One question, maybe it's because I don't know enough about trie. Do you know why people call the function "search" (example below is
solve
) withsearch(1, 30)
orsearch(0, 29)
?Example: Code
I don't understand why the guys are using 29 (or 30) and not any other number. I think this is kind of Level of the tree. Can you help?
Thanks!
Because the max number in the array will be ($$$2^{30} - 1$$$)
and in the trie we traverse on bits so we have maximum 30 bits to traverse on it, think about binary representation.
Can i Know how the size like 'trie[10000006][2]' has been defined.
why 10000006 ?
you have at max $$$10^5$$$ numbers and each number could make 30 child(number of bits) and for each one of them 2 values are assigned to them(which is 0 or 1)
So at the end you need $$$10^5$$$ $$$*$$$ $$$30$$$ $$$*$$$ $$$2$$$
Yup.
Code If anyone's interested
Not understanding the editorial of E, can some one elaborate more on the approach? thanks!
Yes, I also don't really understand the solution after the part about building the initial union of all segments.
Any help is much appreciated thanks.
I was also a little confused before I read the code.
Instead of building the initial union, the code
get()
s all the right borders of the union,ls
, and the initial number of segments,cur
.And while you
process()
the intervals(i.e. scanning them), you check if there is there is exactly a open segment $$$x$$$ before the current right segment, and if there is,++ans[x]
(the difference if you remove x). Otherwise, the current right border would not appear in any answer.Finally, you
solve()
the question by checking if removing segment $$$x$$$ will remove a right border inls
, then addingans[x]
tocur
.Thanks!
can you please explain what does it mean by "adding update" in the editorial of E?
Can anyone find the mistake in my code? I got the wrong answer on test 63 and I have no idea how to fix it. 68545474
Please correct me if I'm wrong but in the code for D above
Isn't the line
if (l.size() == 0) return solve(r, bit - 1)
meant to be
if (l.size() == 0) return solve(r, bit - 1) + (1 << bit)
EDIT: Sorry, I misunderstood the problem statement I thought we were supposed to find the value of X.
Thanks for an excellent set of problems and a timely well-written editorial!
In problem C, why are there at most 11 distinct primes?
You should read other comments first, to see if your query has already been answered. This comment.
Read more about primorials.
thank u, It's very nice.
Can anyone suggest some other problems similar to Problem D?
What are some good xor problems on codeforces?
Xor and String classic problems
A Beautiful Technique for Some XOR Related Problems
I didn't understand the part where we said that bit will be 1 if both groups will be non-empty. Will anyone give me an example to understand that part of the problem D
Consider 5,6,7 the answer is 2.
The 3rd bit in all of them is set, therefore, we can set the 3rd bit in X to make sure that the 3rd bit in the answer is 0.
The 2nd bit is set in 6 and 7 but not in 5 so no matter we set it in X or not it will be set in the answer because when we XOR it with them there will be a difference between X and at least one of them.
Now 5 is in one group and 6,7 are in another. In the group that consists of only 5 we have the 1st bit set to 1, therefore, we set the 1st bit in X to 1. So X should be either 5 or 7 and the answer is 2.
could u tell me whats wrong in my code , i mean i am using dp for each bit of X with 0 and 1 reflecting the on and off state Code?
<< from editorial : If both groups aren't empty, whatever value we assign to the current bit of X, we will have this bit on in our answer. >>
Let n=2, two numbers are 9(1001) and 5(0101) . And we are considering the 3rd bit (counted from 0). On the value of x ..
if we on that bit .. (so x=1000 now) after xor two numbers will be 1(0001) and 13(1101), ans=13 if we on that bit .. (so x=0000 now) after xor two numbers will be 9(1001) and 5(0101) , ans=9
the 3rd bit is ON in either cases . Hope u got it.
Thanks for such great explanation.
For problem F ,I used an amazing greedy algorithm ,of course it is wrong ,But why I got TLE instead of WrongAnswer? https://codeforces.net/contest/1285/submission/68568056
ok... , I only let 3000=2750 it got WA instead of TLE.
Be careful ,man .Don't expect to pass a problem without the proper solution.
Can someone tell me why this solution is getting timed out for question C? https://codeforces.net/contest/1285/submission/68565564
In for loop use long long instead of integer the variable is overflowing and it is giving TLE
1) https://codeforces.net/contest/1285/submission/68534255 2) https://codeforces.net/contest/1285/submission/68585814
Why the 1st code gives wrong ans for test 7 while the 2nd code works fine ?
( only difference between the two is initial value of ans, LONG_MAX doesn't work while 10^12 works)
Value of LONG_MAX is equivalent to INT_MAX. You should have used LLONG_MAX. Try to print all of them and compare.
Can someone explain how the complexity for problem F is derived?Won't we be looping over a number's divisors multiple times each time it enters the routine to calculate the maximum product of two coprime numbers (to find if there still is a number coprime to it in the stack)?
Can someone explain me the time complexity of problem D ...I came up with the similar approach but was not able to find the time complexity !!!TIA
In each iteration, we are iterating over the array and splitting the array into two subarrays so if the size of the left subarray in the ith iteration is $$$b_i$$$ the recurrence relation in the depth of $$$i$$$ will be $$$T(n) = T(b_i)+T(n-b_i) +cn$$$. Now if you draw the recursion tree, you'll see that the sum of all $$$T(n)$$$ on the same depth is $$$O(n)$$$. The depth of the tree is $$$log(max(a_i))$$$ which is less than 31 and the running time is $$$O(nlog(max(a_i)))$$$.
define finish(x) return cout << x << endl, 0
what does this line do?
It creates the alias for printing a single value $$$x$$$ to stdout and exiting with exit-code 0. For ex, finish("test") will print "test" and terminate program. It's not used in solutions above.
Can someone explain me F? I didn't quite get it
In problem B, why my solution is giving WA? My Submission:https://codeforces.net/contest/1285/submission/68610213 I simply made a segment tree and excluding the node which contains the whole interval [1,n],I checked for all other node if they have sum>=sum of all elements..
{3 -1 3 -1} breaks your solution, because you check all (not all, some of them) segments with length equal to power of two, but answer is [3 -1 3]
Ok ..now I get it...silly of me not to think of the cases..once again thanks a lot!!!
can someone tell what will be the output for problem B for case 9 9 9 -1 9 9 9 and why because im not able to connect with the editorial and the problem
Answer would be "YES" because no matter what sub-array is chosen, there will always be a positive sum left out from the left or right part of the array (elements which are not part of the sub-array). Thus total sum will always be greater than the sub-array sum. In general, if there exists a prefix [1, i] or a suffix[j, n] whose sum is <= 0, then that suffix or prefix can be dropped and the remaining array will have sum >= total sum.
what will happen in 2nd question if the first and last elements of array is zero
Answer comes out to be "NO" because the other guy can just select the interval [2,n-1] and get sum=sum of all elements.
This is an alternate approach for E:
Let us store the segments sorted (first by start, then by end) in seg[1...n]
Define p(i) : Number of segments in union of segments 1, ..., i
Define me(i): max{ seg[1].end, ..., seg[i].end }
Obtaining both of these is not a difficult task and can be performed in O(n)
Now, we process the segments in order: n down to 2 while maintaining a set called V whose definition is: When we start processing segment i, V contains the union of segments i+1...n in sorted order (V contains segments which are union of seg[i+1]...seg[n]). When we start processing segment n, V is empty. It must be clear that at any point V contains segments which are pairwise disjoint.
To calculate the number of segments in union if segment i is removed, find the number of segments in V whose start is greater than me(i-1). This can be achieved by binary search. Let this number be x. Update answer as max(answer, p(i-1) + x). The reason for this is: From segments 1...i-1 the max end point is me(i-1). This covers all segments from V which have start <= me(i-1). The rest of the segments in V are x in number.
To make it clear, we first process segment n (and update V to include segment n) then process segment n-1 (again update V to include segment n-1) and so on. We do not process segment 1. For segment 1, the answer is simply the size of V after processing segments 2...n.
Updating V: After processing segment i, we need to update V. Let (l, r) = seg[i] and segments in V be (l_1, r_1), ..., (l_k, r_k). It must be clear that the segments in V are pairwise disjoint. Also, l <= l_1 <= l_2 ... <= l_k. To add (l, r) to V, keep removing segments from beginning whose l_i <= r (and while doing so, update r = max(r, r_i). Finally you will have (l, r) such that it is disjoint from each segment in V. Add this (l, r) to beginning of V.
I have omitted some minor details.
looks like you haven't tested this algorithm yourself, because i have implemented the exact same logic but this solution gets TLE, i just can't make out.
Can someone please help find whats wrong here : My Submission
Have you implemented it correctly? My implementation runs in 124ms
Please, don't make assumptions 68618583
i figured out the error in my code, fixed it and got it AC, thanks for the nicest and simple solution compared to the editorial
I really like this natural solution. Thanks a lot.
Whoops, sorry, guys, I reread my editorial for E and it seems that I was too tired to write anything :) There are some nice comments detailing it, but I updated mine anyway. Hopefully, now it's easier to get what was going in my head when I wrote the solution. The part with sweepline still sounds pretty complicated but I guess I can't explain it better without doing an entire lecture on what sweepline actually is. The code should be pretty clear, refer to it maybe.
can you explain the meaning of "adding updates" to me please?
A solution for 1285D - Dr. Evil Underscores with trie with an idea like the editorial but more understandable 68656872
For E (Delete a Segment), I try the following:
Can someone tell me if something is wrong with this approach? I seem to be failing at a test case: 68633455
can someone explain this statement from problem F
"That because this number together with a number smaller than x can never give a better product than that of a greater or equal number together with x"
We consider numbers from largest to smallest, so after a number x we will consider numbers < x. Also we keep on adding numbers to the stack in the process. Since we add numbers to the stack in decreasing order, the number of the top will be the smallest, and the number after that will be second smallest and so on.
So, for a particular x, after we pop the stack we will get a larger number on the top, which will get us a better answer for that x. Also we don't need the popped number, say y, anymore; since y * (some_number_smaller_than_x_which_will_get_considered_in_next_steps) will always be less than x * (some_larger_number_than_y_which_appeared_after_popping_out_y).
I failed to implement the sweep line solution on problem E, so instead of that, I just wrote a simple prefix sum and got accepted, lol.
For problem D whats wrong in this dp solution:
Code
For problem F: if anybody else is confused with Mobius function in the editorial (like I was), it's not necessary. Just use inclusion-exclusion by itself, see my submission. Imho, editorial would be much easier if Mobius function was not mentioned at all.
For F, I used to be a little confused about the formula about the number of elements in the stack coprime to $$$x$$$, and I wrote a short expain for this part. I hope it can help someone.
Define $$$S$$$ as the number of elements in stack.
Write $$$d = p_1p_2...p_k$$$ if we can, where $$$p_i$$$ are distinct primes.
Consider $$$cnt_d$$$ as the set of multiples of $$$d$$$ ($$$|cnt_d|$$$ is equal to $$$cnt_d$$$ in tutorial), $$$|cnt_d| = |cnt_{p_1}\cap cnt_{p_2}\cap ...\cap cnt_{p_k}|$$$, where $$$p_i$$$ are distinct primes.
Hence, $$$S-f(x) = \sum_{k>=1, p_i|n} (-1)^{k-1}|cnt_{p_1}\cap cnt_{p_2}\cap ...\cap cnt_{p_k}|$$$, Consider it by using inclusion-exclusion over all factors of x.
By the definition of Mobius function, $$$\mu(d) = (-1)^k \Rightarrow$$$, $$$S-f(x) = \sum_{d>1, d|n} -\mu(d) |cnt_d| \Rightarrow f(x) = S+\sum_{d>1, d|n} \mu(d) |cnt_d| = \sum_{d|n} \mu(d) |cnt_d|$$$, ($$$\mu(d) = 0$$$ for all $$$d$$$ can not write as $$$d = p_1p_2...p_k$$$).
In Problem C, for n = 4 why is a = 2 and b = 2 is incorrect?
cuz LCM(2, 2) is not equal to n = 4
Osama_Alkhodairy Are there a db solution for E? I mean I solved it using recursion with Memoization after sorting the segments by their startpoints then their endpoints, the state is (
idx:
the current index,taken:
did I delete a segment yet or not,max:
the maximum endpoint so far) Isn't this qualifies the problem fordp
tag? I added it but when I checked the tags of the problem a minute ago someone deleted it And I just wanna make sure that my thinking is right before adding the tag again.. so please back me up here or point out why this is not adp
/Memoization
solution.the complexity of the above algorithms is
O(n*log(n))
for sorting the segements thenO(n*2*2)
for the recursion with Memoization, I know thatmax
is between -1e9 and 1e9 but because where are going to delete one segment only so we don't expect more than two different values in the state. note that there is an overhead depending whether we are going to storemax
in a map or in unordered_map like I did.my solution
Can anyone explain why choosing coprime pairs in C gives the optimal answer? I mean can anyone prove it mathematically or logically??? Osama_Alkhodairy
Since the problem requires you to minimize max(a, b), so if a and b are not coprime then there is a smaller answer when you divide one of them by their GCD
I understood that when a and b are coprime, then the solution would be minimum, but how are sure that we would find a and b such that they satisfy being coprime ?
In another words, I got how (d,X/d) should be looped over but how are we sure that there will exist at least a single pair where lcm(d,x/d) == x.
If you think of the prime factorization of x, you can always split it into two integers that have no common prime, therefore their lcm must be their multiplication since there is no gcd between them.
I found this AC submission of problem F from a friend of mine which was submitted from vjudge. The code looked very simple and we were puzzled by how it gave correct output. But we stumbled upon an input where it gave a wrong output.
Input:
5
53508 53508 53508 1248 1078
The output should be 672672, but this code prints 588588
I guess the test cases were weak to pass such a submission :/
In Div2 B, what's wrong with using Kadane's algorithm to get the maximum subarray sum?
It may be a bit late but you can use kadane's algorithm with slight modification in code to solve Div2B first create an array with 0th to the n-2th element of the original array in it then use kadane to find the sum of maximum sum subarray of this newly created array let this be ans1. Then create a second array with 1st to n-1th element of original the array in it and again use kadane to find the sum of maximum sum subarray in it let this be ans2 then ans = max(ans1, ans2) is Adel's tastiness compare it with the total sum of the original array and you are done. Note by creating two different arrays we ignore the possible case in which both first and last element (total original array) was picked kadane's algorithm.
Code — Submission
E can be solved with biconnected components as well.
The segments can be viewed as nodes in a graph. Split the segments in events. When you meet a starting event,connect the current node with the last node in the set and add the current node to the set. When you meet a ending segment connect the current node with the first node in the set and the current node with the last one in the set.
The key of the set should be represented by the time when you added the node and the node itself.
After you create the graph, do biconnected components and find the node which is in the most components. The answer should be the number of connected components — frequency of the node — 1.
There are some edge cases related to duplicates or that the frequency of the node is 1.
https://codeforces.net/contest/1285/submission/72275496
Use TRIE in problem D! https://codeforces.net/contest/1285/submission/76266782
Amazing how "& could turn a TLE to an AC!
My solutions for Problem D...
TLE Code
AC of the above TLE Code with just "&" (Pass by Reference) [826ms]
Another AC using Vector [405ms]
Same code, using Vector runs in 405ms , where as using set runs in 826 ms. Fascinating!
Vector vs List
Gotta say Problem D had pretty Tight Time Constraints! Loved it!
i dont think so because i got accepted with
280ms
without using pass by reference(&) here is my solution similar to tutorial but without pass by reference anywhere ----> https://codeforces.net/problemset/submission/1285/87212507Problem E — "x is also in lfnf[j] because" — shouldn't this be lfnw[j] ?
I solved problem E differently from the editorial: I used a segment tree: for each node, it stores two things: the minimum, and the number of continuous ranges of minimum. For example, for the array $$$[1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1]$$$, the minimum is $$$0$$$ and the number of "packets" is $$$3$$$. Then it is just easy brute force.
104143094
NOT the best editorial. Didn't explain many crucial parts like the time complexity of the recursive function used in Problem D which is actually the main part of the solution. Disappointed
I was up-solving this problem and I also spend sometime trying to understand why the complexity is O(nlog(maxai))
let's fix the log for now to be 30 as we iterating over 30 bits, try to track each number (you can draw a tree for that the max depth of the tree will be 30) and you will notice each number will be inserted to either on/off arrays max of 30 times.
wow, that was a nice way to think!
In editorials of problem D.Time complexity is mentioned O(nlog(max(ai))).can anyone explain how it is ?.As function calling itselt 2 times ,so i think there should be $$${n}*{(2)}^{30}$$$.