LightOJ 1236 Pairs Forming LCM(唯一分解定理)



大致题意:求the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j ).

数据范围:

 T (≤ 200)denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).


思路:把n分解成素因数的形式n=p1^c1+p2^c2+...pm^cm

假设已找到一对(a,b)的lcm=n

有a=p1^d1+p2^d2+...pm^dm

b=p1^e1+p2^e2+...pm^em

易知max(di,ei)=ci

先考虑有序数对(a,b),由唯一分解定理知,a的每一个素因数的幂的大小都决定一个独一无二的数。

所以(a,b)的种数就是(di,ei)的种数,即2*(ci+1)-1(因为有序对(c1,c1)重复了一次所以-1)

所以有序对(a,b)的种数ans=(2*c1+1)*(2*c2+1)*(2*c3+1)*...*(2*cm+1)

但是要求求无序对的种数,已知(a,b)(b,a)重复,除了(n,n)只算了一个之外,所以ans = ans/2 +1

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;
const int N = 1e7+100;
ll prime[N/10];
bool vis[N];
int tot;
void ini()
{
    for(ll i=2;i<N;i++)
        if(!vis[i])
        {
            prime[tot++]=i;
            for(ll j=i*i;j<N;j+=i) vis[j]=1;
        }
}
int main()
{
    ini();
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        ll n;
        cin>>n;
        ll t=n;
        ll ans=1;
        for(int i=0;i<tot&&prime[i]*prime[i]<=n;i++)
        {
            ll cnt=0;
            while(t%prime[i]==0) cnt++,t/=prime[i];
            ans*=(2*cnt+1);
        }
        if(t>1) ans*=3;
        ans = ans/2 +1;
        cout<<"Case "<<cas<<": "<<ans<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值