zoukankan      html  css  js  c++  java
  • (数论)最大公约数和最小公倍数问题

    题目描述 Description

    输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数

    条件:  1.P,Q是正整数

    2.要求P,Q以x0为最大公约数,以y0为最小公倍数.

    试求:满足条件的所有可能的两个正整数的个数.

    输入描述 Input Description

    二个正整数x0,y0

    输出描述 Output Description

    满足条件的所有可能的两个正整数的个数

    样例输入 Sample Input

    3 60

    样例输出 Sample Output

    4

    数据范围及提示 Data Size & Hint

    3 60

    60 3

    12 15

    15 12

    分析
        1. 我们知道:Q=P*y0/x0,令y0/x0=k,则可得到一个等式:Q=k*P。则我们的目的就是找出这个等式成立共有多少种可能;
        2. 为了使等式两边成立,只需把k分解因数,把因数分到等式两边即可,如对于输入样例可得:k=20,可分解成:2*2*5,由于要保证最大公因数不会改变,所以2*2只能当做是一个4放在等式的一边,而不能把两个2分别放在等式两边,否则最大公因数将会变成2*3=6;
        3. 所以,k=20时,只能分解成4*5,也就是20只有两种质因数——2和5,所以2个因数要分到等式两边的情况共有4种,所以答案是4(如表2)。
        4. 所以,此题我们只需把k(y0/x0)进行分解质因数,如果质因数共有n种,则答案就是2的n次方。

     1 #include<stdio.h>
     2 #include<math.h>
     3 int x,y;
     4 bool zhi(int x)
     5 {
     6      for (int i=2;i<=sqrt(x);++i)
     7          if (x%i==0) return 0;
     8      return 1;
     9 }
    10 int main()
    11 {
    12     scanf("%d%d",&x,&y);
    13     if (y%x!=0)
    14     {
    15                printf("0/n");
    16                return 0;
    17     }
    18     x=y/x; y=0;
    19     for (int i=2;i<=x;++i)
    20         if (zhi(i))
    21         { 
    22                    if (x%i==0) ++y;
    23                    while (x%i==0) x/=i;
    24         }
    25     printf("%d/n",(int)pow(2,y));
    26     return 0;
    27 }
    View Code
    随便写写。一点学习心得。。。--如果本文章没有注明转载则为原创文章,可以随意复制发表,但请注明出处与作者
  • 相关阅读:
    LeetCode 516. Longest Palindromic Subsequence
    LeetCode 432. All O`one Data Structure
    LeetCode 450. Delete Node in a BST
    LeetCode 460. LFU Cache
    LeetCode 341. Flatten Nested List Iterator
    LeetCode 381. Insert Delete GetRandom O(1)
    LeetCode 380. Insert Delete GetRandom O(1)
    LintCode Coins in a Line III
    LintCode Coins in a Line II
    LintCode Coins in a Line
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/4166103.html
Copyright © 2011-2022 走看看