CodeForce #318 div 2 C Bear and Poker

很简单的题目啊。。

结果我还真的求了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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值