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. LA 4255 (拓扑排序 并查集) Guess

    设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...

  2. DOM应用

    父级.removeChild(子节点);父级.appendChild(子节点);父级.insertBefore(子节点, 在谁之前); 创建元素: <script> window.onlo ...

  3. jquery总结(1)

    jquery是一种js对象.里面封装了一些方法,但是jquery对象不能直接使用js方法,js对象不能直接使用jquery方法. jquery对象类似于js对象的集合,就是存在形式是以特殊数组的形式: ...

  4. [转]FFMPEG视音频编解码零基础学习方法

    在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...

  5. 新浪微博顶部新评论提示层效果——position:fixed

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. LOL-无双剑姬我的最爱

    LOL打了几年了,是一种娱乐的好方式,但是一个人玩不开黑就很无聊.这游戏最开始我玩的时候无论是赢是输就无所谓的,很高兴的.但是现在输了反而很气愤.也不知道为什么,所以很少玩了. 剑姬对反甲:如果对方出 ...

  7. AJax学习一

    1.Ajax的准备工作,就是要先准备好服务器环境. 这边可以有好几种方式,例如服务器工具: http://www.php100.com/html/plugin/ser/2013/0905/91.htm ...

  8. mysql的password()函数和md5函数

    password用于修改mysql的用户密码,如果是应用与web程序建议使用md5()函数, password函数旧版16位,新版41位,可用select length(password('12345 ...

  9. ubuntu下 apt-get install 下载的文件存放的目录

    apt-get把下载的deb包全部放在/var/cache/apt/archives下面,该目录下的文件可以删除.当然用 sudo apt-get clean 命令也可以,这个命令只会删除缓存起来的d ...

  10. ubuntu 安装AMP环境的笔记 Prefork方式与fast-cgi方法

    具体步骤如下: 系统:ubuntu 8.04 的发行版本 AMP with Prefork(mod-php5)       一.安装APACHE2 # sudo  apt-get  install   ...