您的位置:首页 > 其它

CodeForce #318 div 2 C Bear and Poker

2015-09-24 21:02 357 查看
很简单的题目啊。。

结果我还真的求了n次最小公倍数。其实只用求除了2和3以外的公因数乘积是不是想等就可以啦。

#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std;
int n,st,en;

int gcd(int a, int b)
{
int tmp = a;
if(a<b)
{
a = b; b = tmp;
}
while(b!=0)
{
tmp = a%b;
a = b;
b = tmp;
}
return a;
}
bool check(int a, int b)
{
int c = gcd(a,b);
int x1 = a/c, x2 = b/c;
while(x1>1){
if(x1%2==0) x1/=2;
else if(x1%3==0) x1/=3;
else return false;
}
while(x2>1){
if(x2%2==0) x2/=2;
else if(x2%3==0) x2/=3;
else return false;
}
return true;
}
int main()
{
cin>>n>>st;
int g;
bool find = false;

for(int i = 1; i< n; i++)
{
cin>>en;
if(!check(st,en)){
find = true;
break;
}
}

if(find) cout<<"No";
else cout<<"Yes";
return 0;
}


然而时间差别并不大。

#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<iostream>
using namespace std;
int n,st,en;
int resi(int x)
{
while(x%2 == 0) x/=2;
while(x%3 == 0) x/=3;
return x;
}
int main()
{
cin>>n>>st;
int g;
bool find = false;
g = resi(st);
for(int i = 1; i< n; i++)
{
cin>>en;
if(g!=resi(en)){
find = true;
break;
}
}

if(find) cout<<"No";
else cout<<"Yes";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: