Were you able to solve? If yes can you share test case which was failing for subtask 1? Thanks.
The problem was to reach (a, b, c) from (p, q, r) in minimum number of steps. Therefore, I used recursion + shuffling approach. I created an ArrayList (vector in C++) of pairs (p, a), (q, b) and (r, c). For every iteration, I tried 3 operations:
- Addition operation:
a. Resolve the first pair using addition operator. Letâs call the difference âdiffâ
b. Added this âdiffâ value to the first pair, and called the recursive function for the next iteration.
c. Added the âdiffâ value to the second pair and again, called the recursive function.
d. Finally, added the âdiffâ value to the third pair and performed recursion.
Code Snippet:
Collections.shuffle(p); // p = ArrayList of pairs
long diff = p.get(0)[1] - p.get(0)[0];
p.get(0)[0] += diff; attempt(p, movesSoFar + 1);
p.get(1)[0] += diff; attempt(p, movesSoFar + 1);
p.get(2)[0] += diff; attempt(p, movesSoFar + 1);
-
Multiplication operation:
Code snippet:Collections.shuffle(p); if (p.get(0)[0] != 0 && p.get(0)[1] % p.get(0)[0] == 0) { long fac = p.get(0)[1] / p.get(0)[0]; p.get(0)[0] *= fac; attempt(p, movesSoFar + 1); p.get(1)[0] *= fac; attempt(p, movesSoFar + 1); p.get(2)[0] *= fac; attempt(p, movesSoFar + 1); } -
Finally, the tricky part, tricky multiplication:
Here, I tried to solve the cases which required a tricky way to solve. For example (2, 3, 7) and (17, 22, 42). [(2, 3, 7) * 5 = (10, 15, 35) and (10, 15, 35) + 7 = (17, 22, 42)]. These cases are handled by solving any two equations simultaneously. The value to multiply came as (m = m = (a - b) / (p - q)). I tried to multiply this value and execute the recursive function again.
Code snippet:
// For pairs (a, x) and (b, y), m = (x - y) / (a - b)
long abDiff = p.get(0)[0] - p.get(1)[0], xyDiff = p.get(0)[1] - p.get(1)[1];
if (abDiff != 0 && xyDiff % abDiff == 0) {
long m = xyDiff / abDiff;
p.get(0)[0] *= m; attempt(p, movesSoFar + 1);
p.get(1)[0] *= m; attempt(p, movesSoFar + 1);
p.get(2)[0] *= m; attempt(p, movesSoFar + 1);
}
Pruning: The recursive calls are arrested as soon as âmoveSoFarâ value exceeds the current best âansâ value.
This is quite good and dealing with all cases can be avoided by randomization. Thank you for this. 
Which maths concept did you use?
SeeâŚyou can easily handle the cases where only one or zero operations are requiredâŚthe questionâs hard part is identifying how can we identify all the cases that can be handled using 2 operationsâŚNow you can have 4 types of orderingâŚ
- add & add
- multiply & multiply
- add then multiply
- multiply then add
If you observe carefully the first 2 orders can be only possible if addition required by one of the value is the sum of other two which is a very easy check and similarly second is possible iff mutliplication required by one of the values is product of other two. Now comes the logical partâŚ
case 3 of add then multiply
suppose the initial values are a1,b1,c1 and target values are a2,b2,c2
now you see in this case you are first adding then multiplying so it can be represented in the form of this equationâŚ
(a1+s)*p=a2
(b1+s)*p=b2
(c1+s)*p=c2
(s is some value to be added and p is some value to be mutliplied)
these are equations of some lines and you can find the point of intersection of these lines and check it with third
same is the case with a1 * p + s=a2.
here is my submission which got me AC
https://www.codechef.com/viewsolution/34128963
p.s ignore the function is2PossibleBF() it is brute force approach for first test case
I am a beginner and i m unable to understand this line in code
FORE(mask,1,7)⌠in post contest editorial session i understood it is for making subsets⌠but exactly how? pls explain
I initially solved with conditions including zeros; but later I removed them thinking it would wouldnât affect my code, and I was wondering why I keep getting a wrong answer.
All your test cases, except those with zeros, pass through my code.
Thanks
Hey pavan! could you also tell what should be the answer for each case.
Thanks for the detailed explanation <3 , I did it using similar approach but wasnât able to debug after it was getting Wrong Answer
One of those codes where you just see #include<> and coffin dance music starts playing in your mind.
Happy to help 
Hello Geeks,
Can anyone help me to understand what is the use of this line in editorials solution?
FOR(j,3)if(mask&(1<<j))all.pb(j);
- Perform that operation, and call the function itself with the new parameters. ⌠y we have to do this?
it is used to create different subsets from this set of initial values(a1,a2,a3) . u can read more here . itâll help
Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person) - GeeksforGeeks
why does this not allow me to submit, like other contest questions show the
submit (practice)
buttonâŚso why is it that I cannot do the same for these questions
Worst problem seen on Codechef. Actual solution so specific and another solution (which most have done) take days to implement. Didnât expected this type of problem. Long Challenge doesnât mean that you will make problem that take days to implement. We are not here to do donkey work.
They solved it by doing donkey work i.e., by handling every case separately. This is not that I expected from CodeChef. These type of problems take days to implement.
Well it depends, ranked 1 user of div 2 solved it in less then 80 lines.
Also few questions are always there which are debatable. Though i did expected a neat solution.