http://lightoj.com/volume_showproblem.php?problem=1236

Pairs Forming LCM

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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,求使得lcm(i, j) = n, (i, j)这样的数对有多少种,其中i<=j;(lcm(i, j)表示i,j的最小公倍数)

前几天才写得唯一分离定理的,然而并没有想到这道题与唯一分离定理有什么关联(问了学姐才知道),还是定理没有理解透彻,唉~

求约数,倍数,质因数,gcd,lcm,都应该想到这个定理的

算术基本定理(唯一分离定理)的内涵用一句话来概括就是:

一个数的每一个质因子的不同幂对应不同的因数。

 我们可以利用唯一分离定理:
n = p1^x1*p2^x2*p3^x3*...*ps^xs;
n = lcm(i, j);
假设n = p1^x1;那么i、j有两种:
(1)i = p1^x1,则 j = p1^m(m属于[0,x1]),  这样(i,j)共有  (x1 + 1)种
(2)j = p1^x1,则 i = p1^n(n属于[0,x1]),  这样(i,j)共有  (x1 + 1)种
那么总共就有ans = 2*(x1 + 1)种,又因为当m = n时(1)和(2)这两种情况是一样的,所以最终总情况ans-1,即ans = 2*(x1 + 1) - 1 = 2*x1 + 1
当n = p1^x1*p2^x2*p3^x3*...*ps^xs时总情况ans = (2*x1+1)*(2*x2+1)*(2*x3+1)*...*(2*xs+1);
上面求的ans是i>j和i<j都可以即(i,j)和(j,i)重复了(除了(n,n)只算了一种),而题中求的是i<=j,所以ans /= 2;
还有一种(n,n)的情况得加上
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
const int N = 1e7 + ;
typedef long long ll; int prime[], k;
bool Isprime[N]; void Prime()
{
k = ;
memset(Isprime, true, sizeof(Isprime));
Isprime[] = false;
for(int i = ; i < N ; i++)
{
if(Isprime[i])
{
prime[k++] = i;
for(int j = ; i * j < N ;j++)
Isprime[i * j] = false;
}
}
}//素数筛选 int main()
{
int t, p = ;
ll n;
Prime();
scanf("%d", &t);
while(t--)
{
p++;
int x;
ll ans = ;
scanf("%lld", &n);
for(int i = ; i < k && prime[i] * prime[i] <= n; i++)
{
x = ;
if(n % prime[i] == )
{
while(n % prime[i] == )
{
x++;
n /= prime[i];
}
}
ans *= ( * x + );
}
if(n > )
ans *= ;
printf("Case %d: %lld\n", p, ans / + );
}
return ;
}

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)的更多相关文章

  1. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

  2. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  4. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

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

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  6. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  7. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  8. LightOJ 1236 Pairs Forming LCM【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...

  9. LightOJ 1220 Mysterious Bacteria(唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1220 Mysterious Bacteria Time Limit:500MS     Memo ...

随机推荐

  1. github概念和实战

    fork: 通过fork操作,你将拥有了别人创建的repo的ownership,但是url却变成了/youraccount/repo,这时你将可以做git push操作 clone: 该命令是直接将r ...

  2. Debian字符模式下修改显示分辨率

    Debian字符模式下修改显示分辨率 一.准备工具 a) Git apt-get install git 二.获取屏幕修改辅助软件 a) 创建临时文件 mkdir /tmp/screenModify ...

  3. PHP无法加载MySQL模块

                在 将PHP根目录下libmysql.dll复制到c:\Windows\system32中 在Apache目录中的conf\httpd.conf 中加载libmysql.dll ...

  4. window+git+AndroidStudio+github

    1. 安装配置git 安装:需要从网上下载一个,然后进行默认安装即可.安装完成后,找到 “Git Bash”,点击: 配置: 注意:name和email 只是用来标识身份,但是一定要配置好 2. St ...

  5. HDU 3555 Bomb (数位DP-记忆化搜索模板)

    题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...

  6. [转载] FFMpeg的码率控制

    mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在google上search了一下,这方面 ...

  7. linux各种查看端口号

    1.  查看端口占用情况的命令:lsof -i    [root@www ~]# lsof -i         COMMAND PID USER FD TYPE DEVICE SIZE NODE N ...

  8. 【转】bash调试经验

    原文网址:http://blog.csdn.net/yfkiss/article/details/8636758 bash是Unix/Linux操作系统最常用的shell之一,它非常灵活,和awk.c ...

  9. 【转】lua Date和Time

    time和date两个函数在Lua中实现所有的时钟查询功能.函数time在没有参数时返回当前时钟的数值.(在许多系统中该数值是当前距离某个特定时间的秒数.)当为函数调用附加一个特殊的时间表时,该函数就 ...

  10. library cache lock和cursor: pin S wait on X等待

    1.现象: 客户10.2.0.4 RAC环境,出现大量的library cache lock和cursor: pin S wait on X等待,经分析是由于统计信息收集僵死导致的.数据库在8点到9点 ...