原题链接: https://codeforces.com/problemset/problem/1374/B
测试样例
Input 7 1 2 3 12 12345 15116544 387420489 Output 0 -1 2 -1 -1 12 36
Note
解释 15116544 这个样例 :
除以 6 得到 2519424;除以 6 得到 419904;除以 6 得到 69984;除以 6 得到 11664;乘以 2 得到 23328;除以 6 得到3888;除以 6 得到 648;除以 6 得到 108;乘以 2 得到 216;除以 6 得到 36;除以 6 得到 6;除以 6 得到 1.解题思路: 很简洁明了的一道题。我们要想让 n n n变小,就只能通过消去 6 6 6来解决,而 6 = 2 × 3 6=2\times 3 6=2×3。如果没有 2 2 2我们可以乘以 2 2 2,如果没有 3 3 3,那就是真不行。 利用这个我们即可以进行逻辑判断模拟解决。
AC代码
/* *邮箱:unique_powerhouse@qq.com *blog:https://me.csdn.net/hzf0701 *注:文章若有任何问题请私信我或评论区留言,谢谢支持。 * */ #include<bits/stdc++.h> //POJ不支持 #define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增 #define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。 #define pb push_back #define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define fi first #define se second #define mp make_pair using namespace std; const int inf = 0x3f3f3f3f;//无穷大 const int maxn = 1e5;//最大值。 typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; //*******************************分割线,以上为自定义代码模板***************************************// int t; ll n; int main(){ //freopen("in.txt", "r", stdin);//提交的时候要注释掉 IOS; while(cin>>t){ while(t--){ cin>>n; int cnt=0; while(n%3==0){ if(n%6==0){ n/=6; cnt++; } else{ n*=2; cnt++; } } if(n==1){ cout<<cnt<<endl; } else{ cout<<-1<<endl; } } } return 0; }