Codeforces Round #565 (Div. 3) A. Divide it!

Title: https://codeforces.com/contest/1176
ideas: Piggy
Operation \ (1 \) : \ (n-= \ FRAC {n-2}} {\) , will bring \ (1 \) operations
Operation \ (2 \) : \ (n-= \ {2 FRAC \ Times {n-3}} \) , will be able to perform the operations performed after the \ (1 \) , thus bringing \ (2 \) operations
operation \ (3 \) : \ (n-= \ {FRAC. 4 \ n-Times. 5}} {\) , will be able to perform the operations performed after the \ (2 \) a \ (1 \) , thus bringing \ (3 \) operations
So try to execute the operation will bring less operation

#include<bits/stdc++.h>
 
using namespace std;
 
#define DEBUG cout<<"DEBUG"<<endl
#define mem(str,val) memset(str,val,sizeof str)
 
typedef long long ll;
typedef unsigned long long ull;
 
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
 
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pii> vpii;
 
ll solve(ll n)
{
    ll cnt=0;
    while(1)
    {
        if(n%2==0) cnt+=1,n/=2;
        else if(n%3==0) cnt+=2,n/=3;
        else if(n%5==0) cnt+=3,n/=5;
        else break;
    }
    if(n==1) return cnt;
    else return -1;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--)
    {
        ll n;
        cin>>n;
        cout<<solve(n)<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/c4Lnn/p/12113805.html