zoukankan      html  css  js  c++  java
  • 除法游戏

    小A和小B是一对好朋友,他们的爱好是研究数字。学过除法之后,他们就发明了一个新游戏:两人各说一个数字分别为a和b,如果a的因子能包含b的所有质数因子,那么A就获胜。但是当数字太大的时候,两个朋友的脑算速度就有点跟不上了。
    现在,请你写个程序,来判断胜负吧:输入两个正整数,表示a和b。如果a的因子包含了b的所有质数因子,则输出"Yes",否则输出"No"(输出时没有引号)。

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    typedef long long ll;
    static const string Y = "Yes";
    static const string N = "No";
    bool isComn(ll a, ll b);
    
    int main(int argc, const char * argv[]) {
        ll a,b;
        cin >>a >>b;
        if (a == b) {
            cout <<Y;
            return 0;
        }
        if (isComn(a, b))// 2 8    1234  3456
            cout <<Y;
        else
            cout <<N;
    
        return 0;
    }
    
    ll gcd(ll a, ll b) {
        if (a == b) return a;
        if (a < b) {
            return gcd(b-a, a);
        } else {
            if (!(a&1) && !(b&1))
                return gcd(a>>1, b>>1) <<1;
            else if (!(a&1) && (b&1))
                return gcd(a>>1, b);
            else if ((a&1) && !(b&1))
                return gcd(a, b>>1);
            else
                return gcd(a-b, b);
        }
    }
    
    bool pow_(ll n,ll k) {//求整数n是不是k的整数次幂
        return (n>0 && fmod(log(n)/log(k),1)==0);
    }
    
    bool isComn(ll a, ll b) {
        ll res = gcd(a, b);//公有质因数的乘积 3*5
        if (res == 1) return false;
        else {
            b = b / res;//判断剩下的因子 5 = 75/(3*5) 是否属于 最大公约数的因子(or)
            if (res%b == 0 || pow_(b, res))  //b > res: res^n == b
                return true;
             else
                return false;
        }
        return true;
    }
    
    
        c = gcd(a, b);
        while(c > 1)
        {
            b = b / c;
            c = gcd(a, b);
        }//跳出while的情况有两种:a b互质,或者 b = 1 
        if(b == 1)//如果b=1,则即最大公约数中包含了其他因子 
    
  • 相关阅读:
    lists and Dictionaries
    捕获鼠标点击 位置移动
    Preventing and Event from Propagation Through a set of Nested Elements
    瀑布流
    Using Function Closures with Timers
    $.getJSON 的用法
    Overlay 遮罩层
    git常见问题
    spring 全局异常处理
    spring 事务
  • 原文地址:https://www.cnblogs.com/i-8023-/p/12092519.html
Copyright © 2011-2022 走看看