PROBLEM LINK:Practice Setter: Himanshu Mishra DIFFICULTY:Cakewalk. PREREQUISITES:Greatest Common Factor and Least Common Multiple. PROBLEM:Given four numbers $N$, $A$, $B$ and $K$. There are $N$ problems labelled from $1$ to $N$. Appy solves each problem whose label is divisible by $A$ but not $B$, while Chef solves all problems whose label is divisible by $B$ but not by $A$. Check if Appy and Chef together solve at least $K$ problems or not. QUICK EXPLANATION
EXPLANATIONNumber of multiples of $x$ in range $[1, N]$ is given by $\lfloor N/x \rfloor$. Appy solves all problems whose label is divisible by $A$, which are $N/A$ problems. But this also includes problems whose labels are divisible by both $A$ and $B$. We know, all such problems are the multiples of lcm(A, B). So, we can just subtract it from $N/A$ to get $N/A  N/lcm(A, B)$ which is the number of problems solved by Appy. Chef solves all problems whose label is divisible by $B$, which are $N/B$ problems. But this also includes problems whose labels are divisible by both $A$ and $B$. We know, all such problems are the multiples of lcm(A, B). So, we can just subtract it from $N/B$ to get $N/B  N/lcm(A, B)$ which is the number of problems solved by Chef. The total number of problems solved by Chef and Appy is $N/A+N/B2*N/lcm(A, B)$. We can simply use an if statement to check if this exceeds $K$ or not. As an exercise, solve the problem, where problems are not labeled from $1$ to $N$, but from $L$ to $R$ which is given in the problem. Finding LCM of two numbers is just the product of two numbers divided by its Greatest Common Factor, which can be easily found using Euclid's GCD method. Time ComplexityTime complexity is $O(1)$ per test case. AUTHOR'S AND TESTER'S SOLUTIONS:Setter's solution Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)
This question is marked "community wiki".
asked 13 Feb, 23:56

the links to the solutions are not working, please fix it and where is L and R in the problem statement? answered 17 Feb, 11:08

This Question is Having Weak Test Cases. See this Discussion FEB19 HMAPPY2 Wrong Logic Gives AC (100/100) !! answered 18 Feb, 17:14

Please correct me if I'm wrong but the Euclid's GCD algorithm doesn't have O(1) time complexity, then how is the time complexity O(1) per test case? answered 23 Feb, 19:20

Working on a different approach, not passing submit, but not sure why yet. Anyway, my method is to do a N mod A and a N mod B, setting results to boolean variables. I then do an XOR on the two booleans and increment win counter if xor is (true=1). Only increment win counter if 1 of the two is dividable. answered 06 Mar, 23:23
