1176A - Divide it - CodeForces Solution


brute force greedy implementation *800

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    k=0
    while n>1:
        if n%2==0:
            n=n//2
            k+=1
  
    
        elif n%3==0:
            n=2*(n//3)
            k+=1

        elif n%5==0:
            n=4*(n//5)
            k+=1
        else:
            k=-1
            break
    print(k)

C++ Code:

// Author : abdelrhman
#include <bits/stdc++.h>
#define forLoop(size) for (size_t i = 0; i < (size); i++)
#define forReverse(size) for (size_t i = (size - 1); i >= 0; i--)
#define forE(v) for (auto k : v)
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fj(size) for (size_t j = 0; j < (size); j++)
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define InputOutput                   \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr);
#define ll long long
#define l long
#define EL cout << "\n";
#define YES cout << "YES" \
                 << "\n";
#define NO cout << "NO" \
                << "\n";
using namespace std;
template<typename T>
ostream & operator <<(ostream& os,deque<T>v){
    forE(v){
    cout << k << " ";
    }
    return os;
}
template<typename P>
ostream & operator <<(ostream& os,vector<P>v){
    forE(v){
    cout << k << " ";
    }
    return os;
}
/*
bool sortbysec( pair<ll,ll> a,pair<ll,ll> b)
{
    return (a.second > b.second);
}
*/
bool sortbyFirst( pair<ll,ll> a,pair<ll,ll> b)
{
    return(b.first > a.first);
}
/*void subString(string s)
{
    int n = s.size();
    for (int i = 0; i < n; i++)
        for (int len = 1; len <= n - i; len++)
            //subStr[s.substr(i, len)]++;
}*/

/*
17 27
-257 -320 -676 -1136 -2068 2505 2639 4225 4951 5786 7677 7697 7851 8337 8429 8469 9343

*/
void fun()
{
    ll n, counter = 0;
    cin >> n;
    while(n != 1)
        {
            if(n % 2 != 0&& n % 3 != 0 && n % 5 != 0){cout << -1 << endl;return;}
            vector<ll>v;
            if(n % 2 == 0)v.push_back(n/2);
            if(n % 3 == 0)v.push_back((n*2)/3);
            if(n % 5 == 0)v.push_back((4*n)/5);
            sort(all(v));
            n = v[0];
            counter++;
        }
    cout << counter << endl;
}
int main()
{
    fast
    InputOutput
    int t = 1;
   cin >> t;
    while(t--)
        {
            fun();
        }
}


Comments

Submit
0 Comments
More Questions

Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making