TWOVSTEN - Editorial

PROBLEM LINK:

Practice
Contest

Author: Denis Anishchenko
Tester: Hasan Jaddouh
Editorialist: Sarv Shakti Singh

Difficulty: cakewalk

Prerequisites: Basic math

Problem: Given a number X, the operation allowed is multiplication by 2, find the minimum number of times the operation has to be performed to make X divisible by 10.

Explanation: The approach is quite simple and can be classified in 3 cases :

  1. If the number is already divisible by 10, then the answer is 0.

  2. If the number is divisible by 5, then the answer is 1. It can be proved,
    Consider N = K * 5, then if we apply the operation once, it becomes, N = K * 5 * 2 which in turn simpllifies to N = K * 10, thus giving the desired result.

  3. If the number doesn’t fall in previous 2 cases, then it is impossible to achieve the desired result, as for a number to be divisible by 10, it has to be divisble by 5 and 2.

Author’s code

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

I hope this will Help You alot :
Just Go Through this

And Subscribe my channel Hello World Please.

1 Like

What if the number is 3?? Wouldn’t the answer be 10 then? Why is it saying -1 in all other cases?

no. because your are thinking of table of 3 it should while in this question you multiply every time by 2

What’s Wrong with my code? can anyone help to figure out? This code gives perfect output but still submission gives WA.

#include<bits/stdc++.h>

using namespace std;

#define nl “\n”

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);

#define loop(i,a,b) for(int i=a;i<b;i++)

#define loope(i,a,b) for(int i=a;i<=b;i++)

int main(){

fastio;

int t;

cin>>t;

while(t–){

int x,count;

cin>>x;

if(x%5!=0)
cout<<-1<<nl;

else {
while(x%10!=0){

  x*=2;

  count++;

}
cout<<count<<nl;

}
}
}