zoukankan      html  css  js  c++  java
  • 洛谷P1029 最小公约数和最大公倍数问题【数论】

    题目https://www.luogu.org/problemnew/show/P1029

    题意:

    给定两个数$x$和$y$,问能找到多少对数$P$$Q$,使得他们的最小公约数是$x$最大公倍数是$y$

    思路:

    我们知道两个数的最小公倍数是他们的乘积除以最大公约数。

    也就是说我们可以把$P,Q$表示成

    $P = k_1x, Q = k_2x, y = frac{PQ}{x}$

    即$k_{1}k_{2}x = y$,且$k_1,k_2$互质

    那么我们只用在$frac{x}{y}$中找到有多少互质的因子就可以了。

    要注意可能$y$不整除$x$,答案就是0

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<map>
     4 #include<set>
     5 #include<iostream>
     6 #include<cstring>
     7 #include<algorithm>
     8 #include<vector>
     9 #include<cmath> 
    10 #include<queue>
    11 
    12 #define inf 0x7f7f7f7f
    13 using namespace std;
    14 typedef long long LL;
    15 typedef pair<int, int> pr;
    16 
    17 LL x, y;
    18 LL gcd(LL a, LL b)
    19 {
    20     if(b == 0)return a;
    21     else{
    22         return gcd(b, a % b); 
    23     }
    24 }
    25 
    26 int main()
    27 {
    28     scanf("%lld %lld", &x, &y);
    29     if(y % x) {
    30         printf("0
    ");
    31         return 0;    
    32     }
    33     LL k1k2 = y / x;
    34     int ans = 0;
    35     for(LL k1 = 1; k1 <= sqrt(k1k2); k1++){
    36         if(k1k2 % k1)continue;
    37         LL k2 = k1k2 / k1;
    38         if(gcd(k1 * x, k2 * x) == x){
    39             ans++;
    40             //printf("%lld %lld
    ", k1, k2);
    41         }
    42     }
    43     printf("%d
    ", ans * 2);
    44     
    45     
    46     return 0;
    47 }
  • 相关阅读:
    Docker ntpdate Permition error
    Sublime+Golang Plugin
    顺序表和链表的区别
    Python 性能优化——对象绑定
    Fix git 提交代码错误
    UVa 10523
    UVa 10551
    UVa 10814
    UVa 10925
    Practice Round China New Grad Test 2014 报告
  • 原文地址:https://www.cnblogs.com/wyboooo/p/10374531.html
Copyright © 2011-2022 走看看