1374B - Multiply by 2 divide by 6 - CodeForces Solution


math *900

Please click on ads to support us..

Python Code:

Test=int(input())
for J in range(Test):
    N=int(input())
    Cont=0
    Aux=0
    while(N!=1):
        if(N%6==0):
            N=N//6
            Cont+=1
        else:
            Aux=N*2
            if((Aux%6==0)or(Aux==6)):
                N=N*2
                Cont+=1
            else:
                Aux=-1
                break
    if(Aux==-1):
        print(-1)
    else:
        print(Cont)

C++ Code:

// Date: 2023-01-31 14:22:25
// Problem: B. Multiply by 2, divide by 6
// Contest: Codeforces - Codeforces Round #653 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1374/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// --------By WIDA--------

#include <bits/stdc++.h>
using namespace std;
namespace WIDA {
	using LL = long long;
	using PII = pair<LL, LL>;
	using TII = tuple<LL, LL, LL>;
	#define FOR(i,a,b) for (int i = (int)(a); i <= (int)(b); i ++)
	#define FOR2(i,a,b) for (int i = (int)(a); i <= (int)(b); i += 2)
	#define FORD(i,b,a) for (int i = (int)(a); i >= (int)(b); i --)
	#define ALL(a) a.begin(), a.end()
	#define rALL(a) a.rbegin(), a.rend()
	#define pb push_back
	#define fi first
	#define se second
	#define sz size()
	const LL INF = 0x3f3f3f3f3f3f3f3f;
	template <class... Args> void _(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    cout << "--->"; int arr[] = {(_(args), 0)...};
	    cout << "\n";
	}
	template <class T> void _i(T args) {
	    cout << "{";
	    for (auto i : args) cout << i << ", "; cout << "}\n";
	}
	template <class T> void _ii(T args) {
	    cout << "{"; 
	    for (auto [i, j] : args) cout << i << " " << j << ", ";
	    cout << "}\n";
	}
	template <class... Args> void __(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    int arr[] = {(_(args), 0)...}; cout << "\n";
	}
	#define int long long // Zmod要关闭
	#define endl "\n" // 交互题要关闭
	const int MOD = 998244353; // 看清楚每道题的 MOD
	const int N = 1e6 + 7; // 二维数组要修改 vector<vector<int> > a(n, vector<int> (m, 0));
}
using namespace WIDA;


bool Solve() {
	int n; cin >> n;
	int cnt = 0, ans = 0;
	while (n % 3 == 0) {
		if (n % 2) ++ ans, n /= 3;
		else n /= 6;
		++ cnt;
	}
	if (n == 1) cout << ans + cnt << endl;
	else cout << -1 << endl;
	return 0;
}
signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	int Task = 1;
	cin >> Task;
	for (int i = 1; i <= Task; ++ i) {
		Solve();
//		cout << (Solve() ? "YES" : "NO") << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle