PROBLEM LINK:
Author:Vaibhav Tulsyan, Aditya Sarode
Tester:Aditya Sarode
Editorialist:Aditya Sarode
DIFFICULTY:
SIMPLE
PROBLEM:
F:
for i from 1 to M inclusive, do
if N is a multiple of K,
divide N by K
else,
multiply N by K
print N
Perform the operation F on N.
QUICK EXPLANATION:
The given pseudo code's complexity will be O(M) and 1<=M<=10^10 Hence, directly implementing the pseudo code will result in TLE.
EXPLANATION:
If N is not divisible by K, then the answer for the problem will be either N or KN. If M is odd, then the answer will be KN, else it will be N If N is divisible by K, then we go on dividing N by K until it is no more divisible by K, once the value of N is not divisible by K, we can easily find out the answer. As, the answer now can be either N or N*K.
Note that while dividing N by K, it should not be divided more than M times.
The pseudo code will be:
if K == 1:
answer = N
else:
while N%K==0 and m!=0:
N = N/K
M = M-1
if M%2==1:
N*=K
answer = N
Complexity: O( log(M)/log(K) )