zoukankan      html  css  js  c++  java
  • 广工校赛——GCD,LCM——我是好人

    Description

    众所周知,我是好人!

    所以不会出太难的题,题意很简单 给你两个数n和m,问你有多少对正整数对最大公约数是n,最小公倍数是m

    最后友情提供解题代码(我真是太好人了)

    void solve()

    {

       long long n, m;

       scanf("%lld%lld", &n, &m);

       int ans = 0;

       for (long long i = 1; i <= m; i++)

       {

          for (long long j = i; j <= m; j++)

          {

               if (gcd(i, j) == n && lcm(i, j) == m) ans++;

          }

       }

       printf("%d ", ans);

    }

    祝大家AC愉快!最好AK,送某扬兑现诺言^_^

    Input

    输入第1行是一个整数T,表示共T组数据。 接下来是T组数据,每组数据占1行,每一行有2个整数n,m(1 <= n, m <= 10000000000),两个数由一个空格隔开。

    Output

    结果输出T行,对应T组数据。(T<=100) 
    每行输出这样的正整数对有多少对(看我多好人,不用你们输出所有整数对)

    Sample Input

    3 1 1 7 10086 4 16

    Sample Output

    1 0 1

    HINT

    大意:设GCD = x,a = k1*x, b = k2*x,因为要使得GCD为x,那么k1,k2要互质,LCM = k1*k2*x,所以m/n=k1*k2,只要找k1,k2满足该式子就行,所以从1开始到根号m/n,找能把m/n分成两个的,且两个互质(即GCD为1),那么复杂度为根号N,注意开LL!
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    typedef long long ll;
    ll GCD(ll x,ll y)
    {
        if(!y) return x;
        else return GCD(y,x%y);
    }
    ll solve(ll n)
    {
       ll sum = 0;
       ll i,temp;
       for(i = 1; i <= (double)sqrt(n*1.0); i++){
            if(n%i == 0){
                temp = n/i;
                if(GCD(i,temp)== 1) sum++;
            }
       }
      return sum;
    }
    int main()
    {
       ll n, m,temp;
       int T;
       scanf("%d",&T);
       while(T--){
       scanf("%lld%lld",&n,&m);
       if(m%n){
            printf("0
    ");
            continue;
       }
        temp = m/n;
       printf("%lld
    ",solve(temp));
       }
       return 0;
    }
    View Code

    GCD的求法:

    int GCD(int x,int y){
        if(!y) return x;
        else return (y,x%y);
    }
    View Code
  • 相关阅读:
    Yii2 urles
    mpdf Could not find image file (http://local.com/xxxxx)
    如何 安装Yii2的高级应用程序模板
    yii2的GridView和ActiveDataProvider具体使用
    Yii2的框架笔记整理
    笔记
    phpstudy本地搭建域名访问
    phpstrom的xdebug开启和yii2下的分页的链接
    一些自己编写的简单的js
    几种经过整理的文件上传压缩和前台js压缩的方法
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4346514.html
Copyright © 2011-2022 走看看