B. Multiply by 2, divide by 6(水题)Codeforces Round #653 (Div. 3)

原题链接: 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 这个样例 :

  1. 除以 6 得到 2519424;
  2. 除以 6 得到 419904;
  3. 除以 6 得到 69984;
  4. 除以 6 得到 11664;
  5. 乘以 2 得到 23328;
  6. 除以 6 得到3888;
  7. 除以 6 得到 648;
  8. 除以 6 得到 108;
  9. 乘以 2 得到 216;
  10. 除以 6 得到 36;
  11. 除以 6 得到 6;
  12. 除以 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代码

/*
*邮箱:[email protected]
*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;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109198917
今日推荐