### Supermagzzz's blog

By Supermagzzz, history, 5 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

• +47

 » 5 months ago, # | ← Rev. 2 →   0 Is there any simpler solution for B?
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 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
•  » » » 5 months ago, # ^ |   +1 I did that , but got wrong answer, could you please help! 112540024
•  » » » » 5 months ago, # ^ |   +2 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 ... ..* ..* 
•  » » » 5 months ago, # ^ |   0 Why is my array subscript wrong from 1, instead of taking module, it is in the form of n-x-1
• »
»
»
5 months ago, # ^ |
0

# include

using namespace std; int main(){ int t; cin>>t; while(t--) { int n,i,j; cin>>n; string A[n][n],B[n][n]; int C[2],D[2],k=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>A[i][j]; if(A[i][j]=="*") { C[k]=i; D[k]=j; k++; } } }

if(C[0]==C[1])
{

int r=(C[0]+1)%n;
int p=(C[1]+1)%n;
A[r][C[0]]="*";
A[p][C[1]]="*";
}

if(D[0]==D[1]) { int r=(D[0]+1)%n; int p=(D[1]+1)%n; A[D[0]][r]="*";

A[D[1]][p]="*";
}
else{
A[C[0]][D[1]]="*";
A[C[1]][D[0]]="*";
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<A[i][j];

cout<<endl;
}
}

}

can anyone tell me what is i'm doing wrong because i'm getting runtime error.

•  » » 5 months ago, # ^ |   0 let's say the '*' are at position ( x1 , y1 ) ans ( x2 , y2 ) respectively if they're on same row or same column (i.e x1==x2 or y1==y2) then print the asterisks in a adjacent row or column of this column else print them at position (x1 , y2 ) and ( x2 , y1 ) ( in this way they'll form a rectangle with sides parallel to the original matrix ) ;
•  » » 5 months ago, # ^ |   0 +1
•  » » 5 months ago, # ^ |   0 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
•  » » » 5 months ago, # ^ |   0 Please post your code so that we can debug it...
•  » » » » 5 months ago, # ^ |   0 https://codeforces.cc/contest/1512/submission/112922007here is my solution for C. I think I have done the same thing but it's not working
•  » » » 5 months ago, # ^ |   0 you can do stress testing between your code and solution code
•  » » » » 5 months ago, # ^ |   0 how can you generate those strings randomly??
•  » » 5 months ago, # ^ |   0 B problemfor testis in range(int(input())): n = int(input()) m = [] x = -1 y = -1 a = [] for i in range(n): l = input() m.append(l) for i in range(n): for j in range(n): if m[i][j]=='*': a.append([i,j]) # print(a) x1 = a[0][0] y1 = a[0][1] x2 = a[1][0] y2 = a[1][1] # print(x1,y1,x2,y2) if x1==x2: if x1>0: x3 = x1 -1 x4 = x1 -1 y3 = y1 y4 = y2 else: x3 = x1 + 1 x4 = x1 + 1 y3 = y1 y4 = y2 elif y1 == y2: if y1>0: y3= y1 -1 y4 = y2 -1 x3 = x1 x4 = x2 else: y3 = y1 + 1 y4 = y2 + 1 x3 = x1 x4 = x2 else: x3 = x1 y3 = y2 x4 = x2 y4 = y1 for i in range(n): s = "" for j in range(n): if i==x1 and j==y1: s = s + '*' elif i==x2 and j==y2: s = s + '*' elif i==x3 and j ==y3: s = s + '*' elif i==x4 and j==y4: s = s + '*' else: s = s + '.' print(s) 
•  » » 5 months ago, # ^ |   0 Don't know if you will found this simpler but you can do like note the two cordinates which are star and if they are not in same row or column simply exchange the x cordinate and y cordinate resp to find the other two cordinates and if they are in same row or column simple increase the cordinates which are same suppose in same row incrment the row cordinate by 1 modulo n.
•  » » 5 months ago, # ^ |   0 112627220 check mine hope you will get it
•  » » 5 months ago, # ^ |   0 find the rows columns of given stars as r1,c1, and r2,c2. if r1==r2 then using boundary conditions increment or decrement. do the same thing for c1 and c2. then new stars indexes will be r1c2 and r2c1. https://codeforces.cc/contest/1512/submission/112609651^ | my submission.
•  » » 5 months ago, # ^ |   0 Can do simply this way... ~~~~~ void Solve() { int n; cin >> n; char mat[n][n]; int row[2], col[2]; int m = 0; forn(i, 0, n) { forn(j, 0, n) { char x; cin >> x; mat[i][j] = x; if (x == '*') { row[m] = i; col[m] = j; m++; } } } if (row[0] == row[1]) { mat[(row[0] + 1) % n][col[0]] = '*'; mat[(row[0] + 1) % n][col[1]] = '*'; } else if (col[0] == col[1]) { mat[row[0]][(col[0] + 1) % n] = '*'; mat[row[1]][(col[0] + 1) % n] = '*'; } else { mat[row[0]][col[1]] = '*'; mat[row[1]][col[0]] = '*'; } forn(i, 0, n) { forn(j, 0, n) { cout << mat[i][j]; } cout << endl; } } ~~~~~
•  » » 5 months ago, # ^ |   0
 » 5 months ago, # |   0 the problems were really educational , and I guess codeforces in now competing with at atcoder regarding how fast can they publish editorial XD ( at least for beginner/div3 rounds ) . time to upsolve .
 » 5 months ago, # |   +26 Videos out for E, F, GEFG
 » 5 months ago, # |   +21 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.
•  » » 5 months ago, # ^ |   0 i tried that but some test cases r failed ,please let me know my mistakes ,my solution 112546189
•  » » » 5 months ago, # ^ |   +1 you took everything long long except the sum variable lol 112575126
•  » » 5 months ago, # ^ |   0 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
•  » » » 5 months ago, # ^ |   0 It is testcase 1 of second testcase, you answer -1 where the answer is 1: 1 1 1 1 1 
•  » » » » 4 months ago, # ^ |   0 can you please find the mistake in this 116030741i am following the same method that you mentioned.
 » 5 months ago, # |   0 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.cc/contest/1512/submission/112547774 Verdict: wrong answer jury found answer, but participant did not (test case 88)
•  » » 5 months ago, # ^ |   0 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.
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 1 8 2 ?????0000? correct ans is 1000000001
•  » » » » 5 months ago, # ^ |   0 Thank you!
•  » » » » 5 months ago, # ^ |   0 I got that one correct, but still getting wrong answer on test case 2, can you check mine as well? Thank you. https://codeforces.cc/contest/1512/submission/112930573
 » 5 months ago, # | ← Rev. 4 →   +67 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.)
•  » » 5 months ago, # ^ |   +41 After being hacked and dropping to 344th place, it's nice to be back on top B)
•  » » » 5 months ago, # ^ |   0 How did so many good coders like you got hacked on A?
•  » » » » 5 months ago, # ^ |   +3 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.
•  » » » 5 months ago, # ^ |   +5 Can you please tell all the extensions name on the last 3 rows? How can I get those extension. Please reply.
•  » » » » 5 months ago, # ^ |   +11 Here is the link to the extension : https://chrome.google.com/webstore/detail/carrot/gakohpplicjdhhfllilcjpfildodfnnn?hl=en
•  » » » 5 months ago, # ^ | ← Rev. 2 →   +5 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.
•  » » 5 months ago, # ^ |   +39 just git gud lmao
•  » » 5 months ago, # ^ |   +13 Yep, sorry for it. It is fixed now: https://codeforces.cc/blog/entry/89476#comment-778860
 » 5 months ago, # |   0 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
•  » » 5 months ago, # ^ |   0 s = ???11? a = 2 b = 4In this test case '?' at index 1 & 2 should be converted to '1' first and then '?' at index 0 and 5 should be dealt.
 » 5 months ago, # | ← Rev. 3 →   0 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?
•  » » 5 months ago, # ^ |   0 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.)
•  » » » 5 months ago, # ^ |   0 no its not working!!!!!!!!!!
•  » » 5 months ago, # ^ |   0 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.
•  » » 4 months ago, # ^ |   0 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)).
 » 5 months ago, # |   0 hold on can u declare an array of size 1e7 ?? wouldn't that give run time error
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 You can because it is roughly ~40MB (in case of ints) and in CF the memory limit for most of the problems is 256MB.
 » 5 months ago, # |   +6 Time limits were too tight for problem G. Java and Python users are bound to get TLE, quite unfair.
•  » » 4 months ago, # ^ | ← Rev. 2 →   0 time limits were too tight for G bcz bruteforce with slight optimization gives AC in c++ . submission link
 » 5 months ago, # | ← Rev. 2 →   0 Correct solutions of Problem A are getting Hacked because of wrong test case input..Please disqualify any such hacks..for A..input [which is wrong] 1 7 1 1 1 1 1 2 2 It shows successfull hacking attempt
 » 5 months ago, # |   +1 Problem G with bigger constraint INVDIV
•  » » 5 months ago, # ^ |   +5 Div 3 contests were great when Vohuh was a problem setter.
 » 5 months ago, # | ← Rev. 2 →   0 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.
•  » » 5 months ago, # ^ |   0 1 8 2 ?????0000? correct ans is 1000000001
•  » » » 5 months ago, # ^ |   0 thank you for helping ..
•  » » » » 5 months ago, # ^ |   0 try to seperate if(s[i]='?'&&s[n-i-1]='?') case from rest of the other case and do in separate loop
•  » » » » » 5 months ago, # ^ | ← Rev. 2 →   0 thank you again!! got the error.
•  » » » 5 months ago, # ^ |   0 Thankyou so much brother!
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 My code passes this and a=9 and b=6 one as well but still failing: https://codeforces.cc/contest/1512/submission/112927803
 » 5 months ago, # |   0 What's wrong in the 112530668 code. Is any case I am missing?. Its showing wrong on testcase 42. I think all the cases are taken. Any help will be highly appreciable. Thanks in advance.
•  » » 5 months ago, # ^ |   0 1 9 6 ?????????0?????//ans //101010000010101
 » 5 months ago, # |   0 Can anybody provide knapsack dp solution for problem E;
•  » » 5 months ago, # ^ |   +5 It will give TLE , I guess
•  » » 5 months ago, # ^ |   0 Here is my submission Take a look at this https://codeforces.cc/contest/1512/submission/112546632
•  » » » 5 months ago, # ^ |   0 Can you explain your dp state, what does dp[i][j] denote?
•  » » » » 5 months ago, # ^ |   0 i means no. of elements which add upto j i.e. sum
•  » » » 5 months ago, # ^ |   0 Can you please explain your approach in brief? I am unable to understand. Thank you in advance.
 » 5 months ago, # |   +1 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
 » 5 months ago, # | ← Rev. 2 →   0 Solution for C failing. Can someone help?
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 Try this test Test1 9 6 ??????0??0????1 Answer isn't -1
 » 5 months ago, # |   0 is there any way i could see test case 88 in problem C .
 » 5 months ago, # | ← Rev. 2 →   0 WA4 on D for std::accumulate(.., 0) instead of 0LL I thought C++ is a bit smarter and can convert 0 to 0LL while operating with vectorI am a fool
 » 5 months ago, # | ← Rev. 2 →   +3 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
•  » » 5 months ago, # ^ |   0 same question
•  » » 5 months ago, # ^ |   0 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.
•  » » » 5 months ago, # ^ |   0 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.
•  » » » » 5 months ago, # ^ |   0 The main problem was you were using a multiset of integers.I changed it to long long and it worked.AC submission https://codeforces.cc/contest/1512/submission/112652296I guess have multiset when given arbitrarily large values does not point to end().
•  » » » » » 5 months ago, # ^ |   0 Thank you! That's really weired, I always want to save the memory as more as possible. As you guess, I think it's a good problem to consider.
 » 5 months ago, # |   0 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 ?
•  » » 5 months ago, # ^ |   0 In that block one variable name has a typo: Spoiler else { x3=x1; y3=y2; x4=x2; y3=y1; } 
•  » » » 5 months ago, # ^ |   +3 Thank you very much for going through my code, I tried with the corrected code and it got accepted.
 » 5 months ago, # | ← Rev. 2 →   0 Can someone please help me out in B I have worked out every cases but still it's not working!My concept is that if they are not on the same row or same column then interchange the rows and columnif they are on the same row and the row is 1st row the add the val to both the row indexif they are on the last row then subtract the val from the row indexsimilarly for the same columnshere val=col1-col2 for cases of same rowand val=row1-row2 for cases of same col
•  » » 5 months ago, # ^ |   0 I just had a quick read but it seems:val=abs(col1-col2);this is at most n-1. In your test choose n=5, Place both your stars on the same line, in the middle but at first and last columns row+val and row-val will definitely be out of [0;n-1]
 » 5 months ago, # |   0 112592328 help me problem C
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 112593336 Problem C! 7 12 ??00?101??1?0?100?? 1100110111110110011do this in orderfirst -> ?1 ?0 10 11 00 second -> ?? finaly -> if(n&1) the mid position
 » 5 months ago, # |   0 Since, no high rated contestants tagged the ratings to the problems, how to determine — What could be the problems rating? It's just to assess oneself.
 » 5 months ago, # |   -12 Speedforces.
 » 5 months ago, # |   0  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?
•  » » 5 months ago, # ^ |   0 If the sum is even and less than equal to 2e9 && it is present in the set named "have".
•  » » 5 months ago, # ^ |   0 that just means that after you have subtracted x from the sum, sum should be even so that the question constraint of first n elements sum to n+1 — "This is twice the sum of all the elements in a + x." is satisfied. And then "sum <= 2'000'000'000", we're simply checking if it is an int, to be foundable in "have" multiset. And lastly finding the sum/2 in "have".
•  » » » 5 months ago, # ^ |   0 why we have to check for the sum less than equal to 2e9? and what is this way of writing 2e9 with single quotation marks???
•  » » » » 5 months ago, # ^ |   0 1-> well multiset "have" is of int data type, so must be less that |2^31-1|. 2-> and multiset does evertything that a set does, in addition it allows duplicate elements and remove or insert operation is O(lgn), that's why we use a multiset here.
•  » » » 5 months ago, # ^ |   0 and why we have to use this multiset and accumulate I am coming across this multiset data structures for the first time?
 » 5 months ago, # |   +2 Can someone explain how binary search solution of F works ?
 » 5 months ago, # |   0 In problem C, why are you both looping to the end (i < (int) s.size()) and not half, and doing it twice reversing the liste? The inner loop should be enough? for (int times = 0; times < 2; times++) { for (int i = 0; i < (int) s.size(); i++) { .... } reverse(s.begin(), s.end()); } 
•  » » 5 months ago, # ^ |   0 Yeah, I was thinking this as well.
•  » » » 5 months ago, # ^ |   0 I just tested it. It is accepted: https://codeforces.cc/contest/1512/submission/112957063
 » 5 months ago, # |   0 Can anyone help me by sharing the code of O(n) complexity sieve of problem G!!
•  » » 2 months ago, # ^ |   0
 » 5 months ago, # |   0 112599399 This is my submission of problem F. Please someone tell me whats wrong with this..
•  » » 5 months ago, # ^ |   +1 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.
•  » » » 5 months ago, # ^ |   0 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.
»
5 months ago, # |
+3

# 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.cc/contest/1512/submission/112549540

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

can you check it?

•  » » 5 months ago, # ^ |   0 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:
•  » » » 5 months ago, # ^ |   +2 They tested again, Accepted :)
 » 5 months ago, # |   +2 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?
•  » » 5 months ago, # ^ |   +5 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].$
•  » » 5 months ago, # ^ |   0 Check Second approach in this article for clear understandinghttps://www.geeksforgeeks.org/sum-factors-number/
•  » » » 5 months ago, # ^ | ← Rev. 2 →   0 thank u! got it like...1+p1+p1^2 = p1(p1+1)+1 and so on
 » 5 months ago, # |   0 Can anyone check my C question . I can't find the testcase where I'm wrong. Anyone please help . https://codeforces.cc/contest/1512/submission/112536520Thanks in Advance:)
 » 5 months ago, # |   0 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.
•  » » 5 months ago, # ^ |   +2 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.
•  » » 5 months ago, # ^ | ← Rev. 2 →   +1 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)
•  » » » 5 months ago, # ^ |   0 nice bro, got the intuition now.
 » 5 months ago, # |   0 Can someone please help me out with problem D My idea was to sort the given array and check if the sum of first nth terms is smaller or greater then the last element, if greater then return -1. else to check if sum of first n — 1 terms is equal to a specific element, if not then I tried subtract one element and tried to add n + 1 th element in order to sum reach to n + 2th element. Here is the implementation of the same
•  » » 5 months ago, # ^ |   0 You compared with n+2 element, but if the real sum is in n+1'th position? It is possible last elemnt is x!my idea: if the a[] exist, the sum of a[] can be in position n+1 or n+2. if the sum is in n+1, then x can be 1 to n+2, except n+1. if the sum is in n+2, then x can be 1 to n+1.
•  » » » 5 months ago, # ^ |   0 Thank you, I realise my mistake.
 » 5 months ago, # | ← Rev. 2 →   0 Can anyone tell testcase 2 — 65 for C ? I am getting error on that case
 » 5 months ago, # |   0 If java coder use Arrays.sort() then We got TLE in problem D :) , Please solve this Supermagzzz112527550
•  » » 5 months ago, # ^ |   0 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)
•  » » » 5 months ago, # ^ |   0 Collections.sort() getting Accepted :) bah
•  » » » » 5 months ago, # ^ | ← Rev. 2 →   0 Can you help me? #113113659 I use Collections.sort(), but I is still wrong...
•  » » » » » 5 months ago, # ^ |   0 You can check my solution :+) 112626918
 » 5 months ago, # | ← Rev. 2 →   0 [Problem G] Same Code getting TLE and AC verdict. TLE Code — link AC Code — link Is it a joke?!
•  » » 5 months ago, # ^ |   0 It's happened to me. Got TLE. Then they again tested G, I got AC.
 » 5 months ago, # |   0 Im so happy because I became pupil !!
•  » » 5 months ago, # ^ |   0 Me too
 » 5 months ago, # |   0 Can someone help me find the loophole in this approach for Problem C?It keeps failing Test 2112630518
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 Try this Test1 9 6 ??????0??0????1 Answer isn't -1
•  » » » 5 months ago, # ^ |   0 Thanks! I get my mistake now
 » 5 months ago, # |   0 how to calculate d(a*b) when a%b==0 and b is a prime by using linear sieve of Eratosthenes? Thanks.
•  » » 5 months ago, # ^ |   0 Problem G
 » 5 months ago, # |   0 I tried problem E but I am getting an orange message "out of bounds" on the line 112. Please help, if possible.. Link to my submission https://codeforces.cc/contest/1512/submission/112629138
 » 5 months ago, # |   0 can someone explain why are we choosing elements like this in problem E :: If k>0, high(i,k)≥s and s−i≥low(k−1), then put the number i in the segment [l,r] , decrease s by i, decrease k by 1; Otherwise, we will not put the number i in the segment [l,r].
 » 5 months ago, # |   0 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 ?
•  » » 5 months ago, # ^ |   0 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.
 » 5 months ago, # |   0 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
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 Try this TC!1 7 1 ????000?
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 Test your code on this testcase.23 11??02 1???Hope it helps
 » 5 months ago, # |   0 Can anyone help me ,why my solution is giving wrong answer Problem C.My_Solution
•  » » 5 months ago, # ^ |   0 Try this TC!1 7 1 ????000?
 » 5 months ago, # |   0 Anyone, please post their 1512-C-B-Palindrome solution in Java.
 » 5 months ago, # |   0 Does anyone feels implementation of E is more complicated?My idea is-Let k = r-l+1 Segment length.Take first k numbers in a vector a and let sum = summation of elements in a. Keep last = n.Now we can increase our last_element in a upto n and also increase sum.So while (a.back() < last and sum < s) { a.back()++, sum++ };transfer the last_element to another vector. last--.After the process ends check transferred_elements_sum == s or not.If yes, we find out which elements were unused and we print them with our used elements carefully.Otherwise print -1.Submission
 » 5 months ago, # | ← Rev. 2 →   0 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.
 » 5 months ago, # |   0 In problem G since O(NlogN) is sufficient can someone please tell why can't we do something of typeint fact[1000003]; for(int i=1;i
•  » » 5 months ago, # ^ | ← Rev. 2 →   0 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.
 » 5 months ago, # |   0 Hi,Could someone please help me in knowing which case I am missing in my Solution for Problem — C.I am getting the following error message : wrong answer jury found answer, but participant did not (test case 42)I think my code is not returning any output for testcase-42. Could not figure out it.PS : This was my first time asking a query here. Please forgive me if I missed some info here.Thanks!
 » 5 months ago, # | ← Rev. 2 →   0 It seems like solution of D from editorial doesn't fit in time.UPD Oh, I'm wrong
 » 5 months ago, # | ← Rev. 2 →   0 I would like to point out formatting issues in this post. The reference solution should be put into boxes, but it is not. Also, the reference for E has thousand separators; while it is valid C++, it broke the Codeforces syntax highlighter.
 » 5 months ago, # | ← Rev. 2 →   -11 For problem G, I've tried the O(10^7log(10^7)) solution in python 3 as well as pypy3 but getting TLE(Code attached) However, For C++ editorials, its using an O(n) operation in the second for loopfor (int i = 2; i < N; i++) Isn't this unfair for python guys :( or any suggestions on the below code (apart from switching lang)?(PS: Please be gentle, I'm a novice and might not know all strategies...) #################### start ######################### def all_factors(N): """return count of factors of numbers [1...N] in O(nlogn) time""" fac = [1] * (N + 1) for i in range(2, N + 1): for j in range(i, N + 1, i): fac[j] += i return fac N = 10**7+10 fact = all_factors(N) hmap = {} for i in range(len(fact) - 1, 0, -1): hmap[fact[i]] = i # print('ready...') for _ in range(int(input())): c = int(input()) if c in hmap: print(hmap[c]) else: print(-1) #################### end ######################### MikeMirzayanov Geothermal and all other top coders
 » 5 months ago, # |   0 Can someone help me with problem C? https://codeforces.cc/contest/1512/submission/112930573Not sure what's wrong with my solution...Filled the center if the string has odd length. Filled the single pairs. 0? 1? ?0 ?1 Filled the rest of the pairs. (? on both sides)I am getting wrong answer on test case 2. What could that test case be? Need some hints thanks...
 » 5 months ago, # |   0 In the A-B palindrome solution,why are we reversing the string in for loop?
 » 5 months ago, # |   +1 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?
•  » » 5 months ago, # ^ | ← Rev. 9 →   +1 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.
•  » » » 3 months ago, # ^ | ← Rev. 2 →   +6 $d(n)=(a1+1)⋅(a2+1)⋅...⋅(am+1)$, I think this is not correct.
•  » » » » 3 months ago, # ^ | ← Rev. 2 →   +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!
•  » » 3 months ago, # ^ |   +6 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})$
•  » » » 3 months ago, # ^ |   0 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)
 » 5 months ago, # |   0 Why is E not doable with knapsack? https://codeforces.cc/contest/1512/submission/113093207 Passed sample cases but WA on TC2
»
5 months ago, # |
0

113151207

### This is my approach , it works but i think i could do better logic wise , any one with a better approach please help me

 » 5 months ago, # |   0 Why task E has 1900 rating? It's quite simple
 » 5 months ago, # | ← Rev. 2 →   0 deleted
»
5 months ago, # |
Rev. 2   0

for problem almost rectangle1512B][SUBMISSION:113711769 - Almost Rectangle my output is matching with the recquired one but submission gives wrong answer please tell why? //my code:

# include<bits/stdc++.h>

using namespace std; int main() { int t,n,i,j; stack<pair<int,int>>st;

cin>>t;
while(t--)
{
cin>>n;
char ar[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>ar[i][j];
if(ar[i][j]=='*')
st.push({i,j});}
int x1=st.top().first,y1=st.top().second;
st.pop();
int x2=st.top().first,y2=st.top().second;
if(x1!=x2&&y1!=y2){
ar[x1][y2]='*';
ar[x2][y1]='*';
}
if(x1==x2){
ar[(x1+1)%n][y2]='*';
ar[(x1+1)%n][y1]='*';
}
if(y1==y2){
ar[x1][(y1+1)%n]='*';
ar[x2][(y1+1)%n]='*';
}
for(i=0;i<n;i++){
for(j=0;j<n;j++)
cout<<ar[i][j]<<" ";
cout<<endl;}

}

}

»
5 months ago, # |
Rev. 2   0

I find a new solution of Problem C. awa ~~~~~

# include <bits/stdc++.h>

using namespace std;

string solve(int a, int b, string str) { if (a + b != str.size()) return "-1"; if (a % 2 && b % 2) return "-1"; for (int i = 0; i < str.size() / 2; i++) { char &A = str[i], &B = str[str.size() — 1 — i]; if (A != B && A != '?' && B != '?') return "-1"; else if ((A == '?' && B != '?') || (A != '?' && B == '?')) if (A == '0' || B == '0') a -= 2, A = B = '0'; else b -= 2, A = B = '1'; else if (A == '0' && B == '0') a -= 2; else if (A == '1' && B == '1') b -= 2; } for (int i = 0; i < str.size() / 2; i++) { char &A = str[i], &B = str[str.size() — 1 — i]; if (A == '?' && B == '?') if (a > 1) a -= 2, A = B = '0'; else if (b > 1) b -= 2, A = B = '1'; } if (str.size() % 2) { char &final = str[(str.size() — 1) / 2]; if (final == '0') a--; if (final == '1') b--; if (final == '?') if (a) final = '0', a--; else if (b) final = '1', b--; } if (a == 0 && b == 0) return str; return "-1"; }

int main() { if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin);

int t;
cin >> t;
while (t--) {
int a, b;
string str;
cin >> a >> b >> str;
cout << solve(a, b, str) << endl;
}

return 0;


}

~~~~~

 » 5 months ago, # |   0 Can some one suggest some edge or additional testcases for problem D...Iam getting wrong ans on 86th testcase
 » 5 months ago, # | ← Rev. 2 →   0 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"<
 » 5 months ago, # |   0 Can someone help and tell me why my solution is failing at test case 13 for prob Dhttps://codeforces.cc/contest/1512/submission/115064448
 » 5 months ago, # |   0 Can you help me finding why I am getting WA in this code?? https://codeforces.cc/contest/1512/submission/115197444
 » 4 months ago, # |   0 G is such a great question!
 » 4 months ago, # |   0 #include using namespace std; long long q, j, x, n, a[10000001], f[10000001]; 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<
•  » » 4 months ago, # ^ |   0 oh..just forget what i said ,hahaha
 » 4 months ago, # | ← Rev. 4 →   0 Could someone please help me out with part C? Submission 115966879I failed test case 42. Apparently, my program outputs '-1' while the right output is 101010000010101.As suggested by comments, I tried out "1 9 6 ?????????0?????", but I got the output 000110101011000 instead of -1. Could someone please provide the right test case or point out what is wrong with my code?
 » 4 months ago, # |   0 Can someone explain me the given solution from problem E? I read the tutorial and implemented the solution in a different way, while the given solution does a lot of stuff that aren't said in the tutorial.Here are the questions:1) Why are we searching for the first number first to be used in the subset? for (int first = 1; first + (r - l) <= n; first++) 2) Why are we computing sum as the max sum we can get from a sequence ranging from: first to first + (r-l)? The tutorial said that we want a maximum sum we can get from a sequence ending at a certain number to be >= s, why is this solution instead checking that this maximum sum sum is <= s? Similarly why do we want s - sum <= r - l + 1? int sum = 0; for (int i = l; i <= r; i++) { sum += first + (i - l); } if (sum <= s && s - sum <= r - l + 1) 3) What is needArr? Why after an index >= needArr instead of computing ans[i] = first + (i - l) we actually want to add 1 to a[i] so a[i] = first + (i - l) + 1? int needAdd = r - (s - sum) + 1; for (int i = l; i <= r; i++) { ans[i] = first + (i - l); if (i >= needAdd) { ans[i]++; } non_blocked.erase(ans[i]); } Any help would be appreciated, thanks in advance!
 » 4 months ago, # |   0 Hi, I'm pretty confused on my solution for B. It feels like I have a correct implementation; however, the judge says I have a RUNTIME ERROR: Exit code is -1073741819. It does not offer any additional information, so I was wondering if anyone had any ideas for how to go about debugging this issue?My solution is here: https://codeforces.cc/contest/1512/submission/117051385.lms: "long long multiset"Thank you very much in advance.
 » 4 months ago, # |   0 in problem D on testCase 17 i got TLE in java how to remove it. i have done it in O(nlogn)- 1. [submission:117376993][link of solution is ](https://codeforces.cc/contest/1512/submission/117376993)
 » 4 months ago, # |   0 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).
•  » » 4 months ago, # ^ |   0 my solution is fairly straightforward and understandable. you can check out here
•  » » » 4 months ago, # ^ |   +1 Thanks, It helped.
 » 3 months ago, # | ← Rev. 3 →   0 null
 » 3 months ago, # | ← Rev. 2 →   0 I liked problem E. By the way, solution code could be shorter. Check my solution.
 » 7 weeks ago, # |   0 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] = 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] = 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!
 » 3 weeks ago, # |   0 127575912 can someone please tell me why I am failing on 88th iteration of 2nd test case ? Question- ab palindrome.