UVA 10892-LCM Cardinality

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of different integer pairs with LCM is equal to N can be called the LCM cardinality of thatnumber N. In this problem your job is to find out the LCM cardinality of a number.


題意:現在給你1個數字C,讓你求有多少對的數字的最小公倍數數是C。


思路:由於數字的最小公倍數是C,那麼他肯定是C的因子,

暴力求出所有C的因子,然後在一步一步進行比較,判斷就行了。暴力。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int n,cnt;
int num[100000];
void chuli(){
    int sum=2;
    cnt=0;
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0){
            num[cnt++]=i;
            if(i==n/i)continue;
            num[cnt++]=n/i;
            }
    sum+=cnt;
    for(int i=0;i<cnt;i++)
        for(int j=i+1;j<cnt;j++)
            if(num[i]*num[j]==__gcd(num[i],num[j])*n){
                //printf("%d %d\n",num[i],num[j]);
                sum++;}
    printf("%d %d\n",n,sum);
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        if(n==1){
            printf("1 1\n");
            continue;
        }
        chuli();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章