E - 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

首先,看到这道题我以为有什么巧妙的数学方法,结果是暴力。。。

先求出所有约数,判断是否相同,放到一个数组里,再一个个试,可以直接用__gcd()(注意)。排除n=1的时候不符合情况。

还有在for 语句中如果初始条件不满足判断条件也不会执行。

#include<bits/stdc++.h>
using namespace std;
int a[1000000];
int main()
{
    int m,n;
    while(cin>>m)
    {
        if(m==0) break;
        else if(m==1)
        {
            cout<<1<<" "<<1<<endl;

        }
        else
        {
            int start=0;int sum=2;
            for(int i=2;i<=sqrt(m);i++)
            {
                if(m%i==0)
                {
                    //cout<<"kao"<<endl;
                    a[start++]=i;

                if(m/i!=i) a[start++]=m/i;
                }
            }
                sum+=start;
                for(int i=0;i<start;i++)
                {
                    for(int j=i+1;j<start;j++)
                    {
                        if(a[i]*a[j]==__gcd(a[i],a[j])*m)
                        {
                            sum++;
                        }
                    }
                }



            cout<<m<<" "<<sum<<endl;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值