zoukankan      html  css  js  c++  java
  • UVa10892

    10892 LCM Cardinality
    A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible
    pairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the
    number of different integer pairs with LCM is equal to N can be called the LCM cardinality of that
    number N. In this problem your job is to find out the LCM cardinality of a number.
    Input
    The input file contains at most 101 lines of inputs. Each line contains an integer N (0 < N  2  109).
    Input is terminated by a line containing a single zero. This line should not be processed.
    Output
    For each line of input except the last one produce one line of output. This line contains two integers
    N and C. Here N is the input number and C is its cardinality. These two numbers are separated by a
    single space.
    Sample Input
    2
    12
    24
    101101291
    0
    Sample Output
    2 2
    12 8
    24 11
    101101291 5

    题意:

           输入正整数n,统计有多少a<=b满足lcm(a,b)=n。输出n以及满足条件的整数对数。

    分析:

           只需要枚举并存储数n的所有因子于数组arr,然后两两组合arr中的数,如果两数的最小公倍数是n则满足条件的整数对加1。

     1 #include <cstdio>
     2 #include <cmath>
     3 #define ll long long
     4 const int MAX_N = 1000;
     5 ll arr[MAX_N + 1]; // 存储所有的因子,从小到大
     6 ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
     7 ll lcm(ll a,ll b){return a * b / gcd(a,b);}
     8 // 找出所有的因子
     9 int find_all(ll n){
    10     int m = sqrt(n + 0.5);
    11     int cnt = 0;
    12     for(int i = 1 ; i <= m ; i++)if(n % i == 0)
    13         arr[cnt++] = i,arr[cnt++] = n / i;
    14     if(cnt >= 2 && arr[cnt - 1] == arr[cnt - 2]) cnt--;
    15     return cnt; // 不同因子的个数
    16 }
    17 ll n;
    18 int main(){
    19     while(scanf("%lld",&n) && n){
    20         int cnt = find_all(n),ans = 0;
    21         for(int i = 0 ; i < cnt ; i++)
    22             for(int j = i ; j < cnt ; j++)
    23             if(lcm(arr[i],arr[j]) == n) ans++;
    24         printf("%lld %d
    ",n,ans);
    25     }
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    第二篇 Flask 中的 Render Redirect HttpResponse
    第一篇 你好,我叫Flask
    redis发布订阅
    redis学习
    mysql+centos7+主从复制
    Linux系统基础优化及常用命令
    vim与程序员
    Shell基本命令
    js bom和dom
    javaScript基础
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5838469.html
Copyright © 2011-2022 走看看