### Supermagzzz's blog

By Supermagzzz, history, 22 months ago, translation, 1512A - Spy Detected!

Author: MikeMirzayanov

Tutorial
Solution

1512B - Almost Rectangle

Author: MikeMirzayanov

Tutorial
Solution

1512C - A-B Palindrome

Author: MikeMirzayanov

Tutorial
Solution

1512D - Corrupted Array

Author: MikeMirzayanov

Tutorial
Solution

1512E - Permutation by Sum

Author: MikeMirzayanov

Tutorial
Solution

1512F - Education

Author: sodafago

Tutorial
Solution

Author: MikeMirzayanov

Tutorial
Solution Tutorial of Codeforces Round #713 (Div. 3) Comments (109)
| Write comment?
 » 22 months ago, # | ← Rev. 2 →   Is there any simpler solution for B?
•  » » 22 months ago, # ^ | ← Rev. 2 →   For both the *'s in the matrix at least one of these 3 conditions will hold: 1. They will form a horizontal edge(both having the same row) 2. They will form a vertical edge(both having the same column) 3. They will form a diagonalAfter that, a rectangle can be made using some basic casework. You can refer to My Submission
•  » » » » I think error is in below line:poin1 = make_pair(0,first_x);It should be like this: poin1 = make_pair(first_x,0);Here is the test case in which your code will fail: 1 3 ... ..* ..* 
•  » » Can someone check my problem C submission and tell the test case I am failing because I tried very hard but not able to figure it out even jury is not showing the test case I failed. Kindly help me I am an absolute beginner your support means a lot to me
•  » » » Please post your code so that we can debug it...
•  » » » you can do stress testing between your code and solution code
 » Videos out for E, F, GEFG
 » We can do D in O(n) by observing two cases: One of the biggest two numbers was choosen as x, and the other one is the sum of all smaller numbers. Any smaller number was choosen for x and the biggest number is the sum of all other numbers.
•  » » i tried that but some test cases r failed ,please let me know my mistakes ,my solution 112546189
•  » » » you took everything long long except the sum variable lol 112575126
•  » » Hi, I did the same, however got wrong answer. I apologize if it's too much to ask, but can you please review my code? I compared your code with mine, and it appears I did all the necessary things and yet code fails on the second test case. My submission — 112881623
•  » » » It is testcase 1 of second testcase, you answer -1 where the answer is 1: 1 1 1 1 1 
•  » » » » can you please find the mistake in this 116030741i am following the same method that you mentioned.
 » Can someone point out the mistake in this? tried so many times but cant find it. the case is also not visible to debug. https://codeforces.com/contest/1512/submission/112547774 Verdict: wrong answer jury found answer, but participant did not (test case 88)
•  » » Can someone check my problem C submission and tell the test case I am failing because I tried very hard but not able to figure it out even jury is not showing the test case I failed. Kindly help me I am an absolute beginner your support means a lot to me.
•  » » » 22 months ago, # ^ | ← Rev. 2 →   1 8 2 ?????0000? correct ans is 1000000001
 » 22 months ago, # | ← Rev. 4 →   Is something wrong with the input validator for A? Perhaps I'm being dumb, but I can't find the error in my hacked solution, and it seems strange to me that the top twenty or so participants from the original ranklist have all been hacked. Moreover, a friend of mine reported that the test case 1 3 1 1 1 gives an "unsuccessful hacking attempt" verdict, when it should give invalid input, leading me to suspect that invalid test cases are being used to hack solutions. (UPD: I'm also hearing that the case 1 3 1 2 100 is successfully hacking solutions, which definitely shouldn't be happening, as there are more than two distinct elements in the array.)
•  » » After being hacked and dropping to 344th place, it's nice to be back on top B) •  » » » How did so many good coders like you got hacked on A?
•  » » » » See my post above--the validator is incorrect, and the inputs used for hacking do not satisfy the constraints of the problem. I'm assuming a fix will be issued sometime later today and everyone's solutions will be restored.
•  » » » Can you please tell all the extensions name on the last 3 rows? How can I get those extension. Please reply.
•  » » » » Here is the link to the extension : https://chrome.google.com/webstore/detail/carrot/gakohpplicjdhhfllilcjpfildodfnnn?hl=en
•  » » » 22 months ago, # ^ | ← Rev. 2 →   Can you tell us which extension are you using to see the rating change on right?? UPD: Sorry for asking again, just saw the comment above.
•  » » just git gud lmao
•  » » Yep, sorry for it. It is fixed now: https://codeforces.com/blog/entry/89476#comment-778860
 » 22 months ago, # | ← Rev. 3 →   CODEll c; cin>>c; //for(int i=0;i<=c;i++) {cout< mp; ll n=i; while(n!=1){ if(seive[n]==1) {mp[n]++;break;} mp[seive[n]]++; n/=seive[n]; } ll sum=1; for(auto x:mp){ sum*=(((ll)pow(x.first,x.second+1)-1)/(x.first-1)); } //deb(i,sum); if(sum==c) ret(i); } ret(-1); why its giving TLE for problem G?
•  » » Do not use pow, I do not know your intended solution but pow can result in both precision error and linear calculation (which makes your whole algorithm $O(n\;maxB)$ if you iterate through $n$ values and use pow to calculate some power $a^b$ in $O(b)$ time instead of $O(log\;b)$ time with binary exponentiation.)
•  » » Well, it should TLE. For every query, you are looping from 2 to c to find appropriate n.Instead, you can for all values of c store the minimum n and then answer queries in constant time.
•  » » Use Binary Exponentiation to calculate the value of a raised to b. pow function has a time complexity of O(n). while Binary Exponentiation has a time complexity of O(log(n)).
 » hold on can u declare an array of size 1e7 ?? wouldn't that give run time error
•  » » 22 months ago, # ^ | ← Rev. 2 →   You can because it is roughly ~40MB (in case of ints) and in CF the memory limit for most of the problems is 256MB.
 » Time limits were too tight for problem G. Java and Python users are bound to get TLE, quite unfair.
•  » » 21 month(s) ago, # ^ | ← Rev. 2 →   time limits were too tight for G bcz bruteforce with slight optimization gives AC in c++ . submission link
 » Problem G with bigger constraint INVDIV
•  » » Div 3 contests were great when Vohuh was a problem setter.
 » 22 months ago, # | ← Rev. 2 →   Problem C : https://ideone.com/dZeurz can someone please tell me a testcase where my solution fails i am not able to figure it out.
•  » » 1 8 2 ?????0000? correct ans is 1000000001
•  » » » thank you for helping ..
•  » » » » try to seperate if(s[i]='?'&&s[n-i-1]='?') case from rest of the other case and do in separate loop
 » Can anybody provide knapsack dp solution for problem E;
•  » » It will give TLE , I guess
•  » » Here is my submission Take a look at this https://codeforces.com/contest/1512/submission/112546632
•  » » » Can you explain your dp state, what does dp[i][j] denote?
•  » » » » i means no. of elements which add upto j i.e. sum
 » Hi! Thanks for the editorial! Can anyone here check my submission for problem C and tell me in which test cases, my program failed? I am trying to find a corner case for so long. If you don't have time to read my code, could you at least give me some counterexamples so I can test my code on them?Thank you!Here's my code: https://ideone.com/TZb6bR
 » 22 months ago, # | ← Rev. 2 →   Solution for C failing. Can someone help?
•  » » 22 months ago, # ^ | ← Rev. 2 →   Try this test Test1 9 6 ??????0??0????1 Answer isn't -1
 » 22 months ago, # | ← Rev. 2 →   Why should we check whether the sum is below 2e9? // the solution code if (sum % 2 == 0 && sum <= 2'000'000'000 && have.find(sum / 2) != have.end()) { I mean that b[i] has a constraint 1 <= b[i] <= 1e9. So if sum > 2e9 then sum / 2 > 1e9, as b[i] <= 1e9, we can't find sum / 2 in b[i]. Thus sum <= 2e9 should be redundant. But I got a wrong answer without it, and passed for adding it. Appreciate for anyone who could explain it.My WA submission: 112590439 My AC submission: 112590535
•  » » same question
•  » » If there exists an array a, the sum of the elements must be in array b.Since the maximum sum can be 1e9 in that case(b_i <= 1e9), so the sum of the elements of a + this sum (again which appears in b) can be at most 2e9.
•  » » » Thank you for your reply, but I still can't understand.I see that. But as I said before, if sum > 2e9, have.find(sum / 2) != have.end() should be false. That's to say the if statement result remains the same without evaluating sum <= 2e9.I am so confused that the only possible exception I would say is b[i] has element larger than 1e9.
•  » » » » The main problem was you were using a multiset of integers.I changed it to long long and it worked.AC submission https://codeforces.com/contest/1512/submission/112652296I guess have multiset when given arbitrarily large values does not point to end().
 » Can Someone help me with B , 112546789 I checked the test cases, and its still hard to decipher, perhaps someone has faced the same problem as me ?
•  » » In that block one variable name has a typo: Spoiler else { x3=x1; y3=y2; x4=x2; y3=y1; } 
•  » » » Thank you very much for going through my code, I tried with the corrected code and it got accepted.
 »  if (sum % 2 == 0 && sum <= 2'000'000'000 && have.find(sum / 2) != have.end()) Can someone please tell me what is the meaning of this condition?
•  » » If the sum is even and less than equal to 2e9 && it is present in the set named "have".
 » Can someone explain how binary search solution of F works ?
 » Can anyone help me by sharing the code of O(n) complexity sieve of problem G!!
•  » »
 » 112599399 This is my submission of problem F. Please someone tell me whats wrong with this..
•  » » The convention might by platform-dependent, but sometimes in C++ (-1)/2 = 0. As a result, if at a point you have already made enough money to buy the computer/take the course for the next level, you won't need to spend an extra day to make the money.
•  » » » Thank you very much.. I was initially adding ai for each day at least once. But as you said if already we have enough money we need not spend a day.
»

# MikeMirzayanov

I submitted G solution and got TLE in test case 1, but during the contest test, 1 was passed.

After final testing, I submitted Previous G's solution and got Accepted.

Contest time code: https://codeforces.com/contest/1512/submission/112549540

After final testing: https://codeforces.com/contest/1512/submission/112601755

can you check it?

•  » » Same with me. In final testing my solution for G gave tle on test 1 which was accepted at the time of contest. Please help:
•  » » » They tested again, Accepted :)
 » In G's code can someone explain this line plz:s[i] = s[i] * d[i] + 1;Like how are we calculating when two same prime divisors comes (for eg. 4=2*2) and why/how it works?
•  » » I got it. they are at first dividing the number by some prime which divides it. d[x] for the number x. (Remember we got the prime using the sieve.)Now, Notice that for ${prime}^{r}$ we will have the divisor function equals${prime}^{r} + {prime}^{r-1} + {prime}^{r-2}+ ..... + 1.$If you think about it for some time then you will realize that that's what the expression $s[i]=s[i]*d[i]+1$ when ran in a loop till it is divisible with "j" is doing.the remaining part "j" (which gives us s[j]) has gcd equals one with this prime exponent so we can using the above expresssion simply multiply it.and get $s[i]=s[i]*s[j].$
 » Where does this comes from. Is there some there theorem? Seems like some property like that of phi function but I can not figure out why.Use the multiplicativity of the function d(n):d(a⋅b)=d(a)⋅d(b) if gcd(a,b)=1.
•  » » Let me explain this...suppose some number has prime divisors a, b, c and all are prime so using above formula we can writed(a.b.c)=d(a).d(b).d(c)Now d(a)=a+1, d(b)=b+1, d(c)=c+1so d(a.b.c)=(a+1)(b+1)(c+1)If we expand the R.H.S we get, d(a.b.c) = a.b.c+a.b+a.c+b.c+a+b+c+1and that is what d(a.b.c) would be if you calculate, so both are equal.Let's take 30 as an example.=> 30 = 2*3*5so using above formula d(30)=2*3*5+2*3+2*5+3*5+2+3+5+1=> d(30)=72 & also d(30)=(2+1)*(3+1)*(5+1)=72.Ask me if you have any doubt.
•  » » 22 months ago, # ^ | ← Rev. 2 →   because if a decomposition of A of this form is p1 ^ a1 * p2 ^ a2 ... and a decomposition of B of this form is q1 ^ b1 * q2 ^ b2 ... then the sum of divisors of A equals to: (1 + p1 + p1 ^ 2 + .... p1 ^ a1) * (1 + p2 + p2 ^ 2 + .... p2 ^ a2) ... -> because no matter how we take numbers from each the parentheses are divisors of A and they are all present.Next-> d (a) = (1 + p1 + p1 ^ 2 + .... p1 ^ a1) * (1 + p2 + p2 ^ 2 + .... p2 ^ a2), d (b) = (1 + q1 + q1 ^ 2 + .... q1 ^ b1) * (1 + q2 + q2 ^ 2 + .... q2 ^ b2) i.e. gcd (a, b) = 1 then no p and q coincide. hence d (a * b) = d (a) * d (b)
•  » » » nice bro, got the intuition now.
 » If java coder use Arrays.sort() then We got TLE in problem D :) , Please solve this Supermagzzz112527550
•  » » I don't know much java , but as much as I have heard Arrays.sort() is implemented using quick Sort and is hence O(N^2)
 » 22 months ago, # | ← Rev. 2 →   [Problem G] Same Code getting TLE and AC verdict. TLE Code — link AC Code — link Is it a joke?!
 » Can someone help me find the loophole in this approach for Problem C?It keeps failing Test 2112630518
•  » » 22 months ago, # ^ | ← Rev. 2 →   Try this Test1 9 6 ??????0??0????1 Answer isn't -1
 » how to calculate d(a*b) when a%b==0 and b is a prime by using linear sieve of Eratosthenes? Thanks.
•  » » Problem G
 » Any proof for problem F ? why is it enough to calculate the number of days we should spend on a certain job a_i to get >= c and repeat this process taking the minimum for all jobs taking into consideration how many days will we stay in all the previous jobs ?
•  » » Consider a pair of days today and tomorrow.If we got enough money to to an exam today it is allways benefical to do it today, never tomorrow.This means by induction that for a given number of exams, there is no better solution than doing the exams as early as possible.
 » Hello, i am very new to codeforces(this was my first contest)My code for problem C works perfectly well on my compiler, running it with valgrind gives no memory errors, but the diagnostics fail multiple test cases(which ran successfully on my compiler). I don't understand this!! Can someone please help me?This is my code:Thank you
•  » » 22 months ago, # ^ | ← Rev. 2 →   Test your code on this testcase.23 11??02 1???Hope it helps
 » 22 months ago, # | ← Rev. 2 →   In problem E, can someone help me understand this:  ans = min(ans, cur + max(0ll, c — bal + a[i] — 1) / a[i]); ll newDays = max(0ll, b[i] — bal + a[i] — 1) / a[i]; cur += newDays + 1; bal += a[i] * newDays — b[i]; I got the idea of what this is trying to do but i can't understand how exactly the mathematical operations are working.
 » In problem G since O(NlogN) is sufficient can someone please tell why can't we do something of typeint fact; for(int i=1;i
•  » » 22 months ago, # ^ | ← Rev. 2 →   This approach will give TLE, due to the second loop. Since starting j from i*i in the sieve of eranthoses prunes a lot of iterations.
 » 22 months ago, # | ← Rev. 2 →   It seems like solution of D from editorial doesn't fit in time.UPD Oh, I'm wrong
 » in problem G the multiplicativity of the function d(n) : d(a⋅b)=d(a)⋅d(b) if gcd(a,b)=1. is their any proof for it?
•  » » 22 months ago, # ^ | ← Rev. 9 →   Write $n=p_1^{a_1} \cdot p_2^{a_2} \cdot ... \cdot p_m^{a_m}$ with $p_i$ primes. Then $d(n)=(1+p_1^1+...+p_1^{a_1})\cdot(1+p_2^1+...+p_2^{a_2})\cdot...\cdot(1+p_n^1+...+p_n^{a_n})$ (This was a wrong formula first, thanks to put_peace for correcting it!)Let {$q_i$} and {$r_i$} be sets of primes with empty intersection. Let $Q=q_1^{b_1} \cdot q_2^{b_2} \cdot ... \cdot q_m^{b_m}$ and $R=r_1^{c_1} \cdot r_2^{c_2} \cdot ... \cdot r_k^{c_k}$. Then $gcd(Q, R)=1$ and $d(Q \cdot R) = d(Q) \cdot d(R)$ using the formula for $d$. This isnt a formal proof, but it outlines the idea for a proof. See also https://en.wikipedia.org/wiki/Euler%27s_totient_function the totient function is similar to $d$ here.
•  » » » 20 months ago, # ^ | ← Rev. 2 →   $d(n)=(a1+1)⋅(a2+1)⋅...⋅(am+1)$, I think this is not correct.
•  » » » » 20 months ago, # ^ | ← Rev. 2 →   Oh, you are totally right. What I wrote was the amount of divisors not the sum of divisors!Your comment below is right. If I replace my error with $d(n)=(1+p_1^1+...+p_1^{a_1})\cdot(1+p_2^1+...+p_2^{a_2})\cdot...\cdot(1+p_n^1+...+p_n^{a_n})$ then it's correct. Thanks for the heads up!
•  » » Just to see how it is correct, $d(p^{a}).d(q^{b}).d(r^{c}) = (1 +p + p^{2} + ... + p^{a}).(1 +q + q^{2} + ... + q^{b}).(1 + r + r^{2} + ... + r^{c})$ if you expand the RHS, you can verify that it will give $d(p^{a}q^{b}r^{c})$
•  » » » yes got it,we can even think of it liked(p^a) as sigma(p^ai) where 0<=ai<=asimilarly, d(p^a.q^b.r^c) = sigma(p^ai.q^bi.r^ci) for all 0<=ai<=a 0<=bi<=b 0<=ci<=cits like all combination of powers till a,b,c which can be written asd(p^a.q^b.r^c) = (1+p+p^2+...+p^a).(1+q+q^2+...+q^b).(1+r+r^2+...+r^c)here the rhs its same as choosing power of p and power of q and power of r (i.e same as getting all possible combinations for d(p^a.q^b.r^c)) same as d(p^a).d(q^b).d(r^c)
 » Why is E not doable with knapsack? https://codeforces.com/contest/1512/submission/113093207 Passed sample cases but WA on TC2
 » 21 month(s) ago, # | ← Rev. 2 →   Can anyone please tell me why I am getting Wrong answer on test 2(test case 392) for problem 1512C (A-B Pallindrome). I tried lot but unable to find a case where it fails.Here is my code : #include using namespace std; #define lli long long int #define ff first #define ss second #define endl '\n' int main() { int t; cin>>t; while(t--) { int a,b; cin>>a>>b; string s; cin>>s; int n=s.size(); int flag=0; if((a+b)!=n) { cout<<"-1"<1) { s[i]='0'; s[n-1-i]='0'; a-=2; } else { s[i]='1'; s[n-1-i]='1'; b-=2; } } } if((a<0)||(b<0)) { cout<<"-1"<
 » G is such a great question!
 » #include using namespace std; long long q, j, x, n, a, f; main(){ n=10000000; for (int i=1; i<=n; i++){ for(j=i; j<=n; j+=i)a[j]+=i; if(a[i]<=n&&!f[a[i]])f[a[i]]=i; } cin>>q; while(q--){ cin>>x; cout<
•  » » oh..just forget what i said ,hahaha
 » Can someone please explain me the solution for E (editorial one), I have been trying for a long time but unable to understand how this condition came :(s — sum <= r — l + 1).
•  » » my solution is fairly straightforward and understandable. you can check out here
•  » » » Thanks, It helped.
 » 20 months ago, # | ← Rev. 3 →   null
 » Could someone explain for me the logic behind this part in the editorial code for problem G, please? that part int j = i; s[i] = 1; while (j % d[i] == 0) { j /= d[i]; s[i] = s[i] * d[i] + 1; } s[i] *= s[j]; I knew it was related to the multiplicativity of d(n) but I couldn't fully understand.And this is the whole code#include using namespace std; const int N = (int) 1e7 + 100; long long s[N]; int d[N]; int ans[N]; int main() { fill(d, d + N, -1); d = 1; for (int i = 2; i * i < N; i++) { if (d[i] == -1) { d[i] = i; for (int j = i * i; j < N; j += i) { if (d[j] == -1) { d[j] = i; } } } } s = 1; for (int i = 2; i < N; i++) { if (d[i] == -1) { d[i] = i; s[i] = i + 1; } else { int j = i; s[i] = 1; while (j % d[i] == 0) { j /= d[i]; s[i] = s[i] * d[i] + 1; } s[i] *= s[j]; } } fill(ans, ans + N, -1); for (int i = N - 1; i > 0; i--) { if (s[i] < N) { ans[s[i]] = i; } } int tests; cin >> tests; while (tests-- > 0) { int c; cin >> c; cout << ans[c] << endl; } return 0; } Thanks in advance!
 » 12 months ago, # | ← Rev. 2 →   Hello I am new to programming. Just finished "1512A — Spy Detected!". And now I read the tutorial. I am wondering is my way more efficient than the tutorial and code showed?My idea was to assume the first element of array is spy. Then compare first element until you hit different element. In that moment we have two options first element is spy or the one we just discovered. if (a!=a[n-1] && a!=[n-2]) is simple way to find out who is spy.I would like a feedback on this idea, is this good approach or not? **I wrote my code in C
•  » » if( arr !=arr) 0 or 1 is different element , if(arr==arr) answer is 1 else 0 . if( arr==arr) element !=arr is the answer !
 » Can someone tell me in 1512G editorial how d(a*b) = d(a)*d(b) where gcd(a,b)=1?
 » In problem D, the condition checking for if(sum == b[n] * 2) is done in some contestant's code. How is that possible? this condition would come only when there are 2 elements in the array b[].But in the statement, it is given that n >= 1 , i.e n + 2 would go for '3'. For b.length > 2, we will always have sum > b[n] * 2 as b[i] for 1 <= i <= n, here b[i] >= 1.
 » 4 weeks ago, # | ← Rev. 6 →   I solved E using simpler approach in O(N) if we have $s$ and $r-l+1=k$, so $\frac{(k)(k+1)}{2}$must be greater or equal to$s$First we will subtract the k Values from s (we will Know later Why) $m=s-\frac{(k)(k+1)}{2}$ and if we try to distribute m oranges in k boxes, then every box will have m/k orange(floor division), and remainder will be r oranges, r