1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > B. Multiply by 2 divide by 6(数学) Codeforces Round #653 (Div. 3)

B. Multiply by 2 divide by 6(数学) Codeforces Round #653 (Div. 3)

时间:2020-12-15 10:45:21

相关推荐

B. Multiply by 2  divide by 6(数学)  Codeforces Round #653 (Div. 3)

原题链接:/problemset/problem/1374/B

题意:有一个数能进行两种操作,除以6乘以2,问经过多少次操作能变成1.若不行则输出-1.

解题思路:我们可以统计2和3的因子,再进行判断即可。

AC代码:

/**邮箱:2825841950@*blog:/hzf0701*注:代码如有问题请私信我或在评论区留言,谢谢支持。*/#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<string>#include<stack>#include<queue>#include<cstring>#include<map>#include<iterator>#include<list>#include<set>#include<functional>#include<memory.h>//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉#include<iomanip>#include<vector>#include<cstring>#define scd(n) scanf("%d",&n)#define scf(n) scanf("%f",&n)#define scc(n) scanf("%c",&n)#define scs(n) scanf("%s",n)#define prd(n) printf("%d",n)#define prf(n) printf("%f",n)#define prc(n) printf("%c",n)#define prs(n) printf("%s",n)#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 fi first#define se second#define mp make_pairusing namespace std;typedef long long ll;typedef long double ld;typedef pair<ll, ll> pll;typedef pair<int, int> pii;const ll inf = 0x3f3f3f3f;//无穷大const ll maxn = 1e5;//最大值。//*******************************分割线,以上为代码自定义代码模板***************************************//int t;ll n;int cnt1,cnt2;//统计2的因子数和3的因子数。int main(){//freopen("in.txt", "r", stdin);//提交的时候要注释掉ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。while(cin>>t){while(t--){cin>>n;cnt1=0;cnt2=0;while(n%3==0){n/=3;cnt1++;}while(n%2==0){n/=2;cnt2++;}if(n!=1){//说明不仅仅只是包含2和3这些因子,那显然不能。cout<<"-1"<<endl;}else{if(cnt1<cnt2){cout<<"-1"<<endl;}else{cout<<cnt1+cnt1-cnt2<<endl;}}}}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。