The first idea of the problem is to see that files on xth hard disk can be retrieved using files on hard disk 1. The file indices differ by exactly (x-1). For example, if at any moment of time, hard disk 1 has files f1, f2, f5 when n = 5, then hard disk 2 has files f(1+2-1), f(3+2-1), f(5+2-1) i.e. f2, and f3 (f7 is discarding). See the table to convince regarding this observation. Thus, the queries of both type can be handled very easily using binary search only provided we are able to find the files in sorted order on hard disk 1 on any day.

Next thing is to notice is that the pattern of files being stored on hard disk 1 will repeat after some time, to be precise after 2^(ceil(log2(n)). Draw a table for n = 8 and n = 5 to get view of it. Next observation is that total number of files we require for all possible days is bounded by 3^ceil(log2(n)). For example: Consider n = 4

0: f1
1: f1, f2, f3, f4
2: f1, f3
3: f1, f2
4: same as 0 i.e. pattern repeats.

The total number of files stored i.e. slots used is (1 + 4 + 2 + 2) = 9, 3^2. You can see this thing for any general n. Complete the table for n=5 and observe the same. Now, we just need to come up with a 3^(ceil(log2(n)) generation algorithm, which gives us the files in sorted order on any day. The memory complexity will also be the same. Many constructive algorithms can exist and you can try to find your own. My solution uses just one of my finding, idea is again similar to binary search i.e. can be divide the files (in sorted order) in half, calculate the files on left side and use it to calculate the files on right side. Basically, I mean to say that if a particular bit is set in “y” and we calculate the lower half files, then the upper half files are basically the mirror image on the lower half files. You can check my “gen” function in the solution code for details.

For each test case, output a single line containing two space seperated integers a and b such that a / b = Pmax - the maximum possible power achievable after transformation and gcd(a,b) = 1

In problem 1
Can we solve by checking the frequency of each number from 1 to 100 and if frequency of every element is even then there will be “draw” otherwise “cyborg” will win?

@vivek_1998299 ALPHA refers to alphabet size, which is 20 here. OR convolution refers to polynomial multiplication where c_0 x^{p_0} \cdot c_1 x^{p_1} = c_0c_1 x ^{p_0 | p_1}. To put it simply the coefficients at p_0 and p_1 affect p_0 | p_1 instead of p_0 + p_1.

This is relevant here in the following manner: assume you know there are c_0 substrings with mask p_0 that begin at “x”. Similarly there are c_1 substrings with mask p_1 that end at “x”. So there will be c_0c_1 substrings with mask p_0 | p_1 that contain “x” irrespective of where it lies.

In problem 2 Next part is to modify the cost function as “length — 2(count of numbers) + (sum of numbers)/k”
I am not able to get this! Can you elaborate a bit more on this?

This problem is quite interesting and it seems the accepted solutions use various different methods. My solution uses the fact that the queries can be reduced to the form of find the position of the a^{th} odd number in the b^{th} row of Pascal’s triangle, due to the pattern that shows up. And it turns out that all the odd numbers are at the positions which are numbers obtained by removing zero or more set bits from the binary representation of b (source).

I too had got the first observation, that its required to calculate for hard disk 1 only. Noticed the pattern too, but couldn’t formulate it in terms of code. Had the idea of binary search, but messed up.

I have used double k to do float division.I understand your logic,But what is wrong with my logic.
My idea is to either choose letter or convert to number.
The cost of one letter is 1 and cost of one number is (-1)+(num/k),so my condition is num/k - 1(cost of number) > 1(cost of one letter) then convert to number else add 1 to sum

Yes we can, though there is a small problem with your solution, the constraints state that ‘A[i][j]’ can also be ‘0’ since 0 doesn’t count towards the score, its frequency doesn’t matter, but you are considering that in your solution. A small change in the last loop of your solution to ignore the frequency of zero gets an ac. CodeChef: Practical coding for everyone