LightOJ 1236 Pairs Forming LCM

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H

 Pairs Forming LCM

Description

Find the result of the following code:

long long pairsFormLCM( int n ) {
   
 long long res = 0;
   
 for( int i = 1; i <= n; i++ )
       
 for( int j = i; j <= n; j++ )
           
if( lcm(i, j) == n ) res++; // lcm means least common multiple
   
 return res;
}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

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

Output

For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

Sample Input

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

Sample Output

Case 1: 2

Case 2: 2

Case 3: 3

Case 4: 5

Case 5: 4

Case 6: 5

Case 7: 8

Case 8: 5

Case 9: 8

Case 10: 8

Case 11: 5

Case 12: 11

Case 13: 3

Case 14: 4

Case 15: 2


题意:给出n,求有多少个数对(i,j),使得LCM(i,j)=n?
思路:比如24=2^3*3^1:
(1)如果一个数完整地包含了3^1但是没有完整地包含2^3(一个数x完整地包含某个质因数p及其出现的次数t,指x可以被p^t整除),比如3,6,12,那么另一个数必须完整地包含2^3,比如8,24。那么此时有六种组合(3,8),(3,24),(6,8),(6,24),(12,8),(12,24)
(2)若一个数完整地包含2^3但是没有完整地包含3^1,比如8,那么另一个数必须完整地包含3^1,比如3,6,12,24,此时有4个。
(3)若一个数完整地包含了2^3和3^1,比如24,那么另一个数有(3+1)*(1+1)种可能,即1,2,3,4,6,8,12,24。
(4)若一个数既没有完整的包含2^3也没有完整地包含3^1,比如1,2,4,那么另一个数必须为24,此时有3种。
到此为止,你发现除了(24,24)这种组合只在(3)中出现一次,其他情况均出现2次。若上面的总数为t,那么答案为(t+1)/2。

n可以表示为:n=p1^x1*p2^x1.....pk^xk。

  假设lcm(a,b) == n;

  a = p1^c1 * p2^c2 ..... pk^ck。

  b = p1^e1 * p2^e2 .... pk^ek。

  xi = max(ci, ei)。

  对于有序数对(a,b),有唯一分解定理知,每一个素因数的幂都决定了一个独一无二的数。

  求(a,b)的种数就可以转化为求(ci,ei)的种数:ans = (2*x1+1)*(2*x2+1).....(2*xk+1)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;

#define N 10000500
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))

typedef long long LL;

int isprim[800000], k = 0;
char a[N] = {1, 1};
LL n;

void prim ();

int main ()
{
    prim ();
    int t, nCase = 1;
    LL n;

    scanf ("%d", &t);

    while (t--)
    {
        scanf ("%lld", &n);
        int i = 0;
        LL ans = 1;

        while (n > 1 && i < k)
        {
            int t = 0;
            while (n % isprim[i] == 0)
            {
                n /= isprim[i];
                t++;
            }

            i++;
            if (t) ans *= 2 * t + 1;
        }

        if (n != 1) ans *= 3;
        //由于素数打表的只能打到sqrt(n)
        //当n不为1的时候说明n还有一个大于sqrt(n)的素因子,所以ans*=(2*1+1);
        printf ("Case %d: %lld\n", nCase++, (ans+1)/2);
    }
    return 0;
}

void prim ()
{
    for (LL i=2; i<N; i++)
    {
        if (!a[i])
        {
            isprim[k++] = i;
            for (LL j=i*i; j<N; j+=i)
                a[j] = 1;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_大太阳_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值