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. 谷歌的ajax.googleapis.com被墙导致访问很多国外网站很慢的解决方法

    比如访问StackOverflow, 更比如flexerasoftware.com(导致Visual Studio的打包程序InstallShield Limited Edition不能注册和下载) ...

  2. 打印机C++

    m_prnDC.SetMapMode(MM_LOMETRIC);  m_iPrnX = m_prnDC.GetDeviceCaps(HORZRES);m_iPrnY = m_prnDC.GetDevi ...

  3. SQLServer的最大连接数 超时时间已到 但是尚未从池中获取连接

    很多做架构设计.程序开发.运维.技术管理的朋友可能或多或少有这样的困惑: SQLServer到底支持多少连接数的并发? SQLServer是否可以满足现有的应用吗? 现有的技术架构支持多少连接数的并发 ...

  4. 关于Java接口

    1 接口的本质 (1)一组有规则的集合: (2)一定视角上的同类事物的抽象:同类事物的概念是相对的 2 接口与抽象类的区别 (1)java不支持类的多继承,但可以实现多个接口: (2)使用动机:抽象类 ...

  5. 剑指offer—算法之位运算(二进制中1的个数)

    位运算: 左移:m<<n将m左移n位,左移后低位补充0: 右移:m>>n将m右移n位,右移后高位补充的是符号位,负数补充1,整数补充0.(正数的边界值为(1,ox7FFFFFF ...

  6. 一:AndEngine的小例子

    首先导入架包,下载:http://download.csdn.net/detail/duancanmeng/4060082 lib文件夹中 像我们写android程序entends Activity一 ...

  7. 深入浅出ghostbuster剖析NodeJS与PhantomJS的通讯机制

    深入浅出ghostbuster剖析NodeJS与PhantomJS的通讯机制 蔡建良 2013-11-14 一. 让我们开始吧 通过命令行来执行 1) 进行命令窗口: cmd 2) 进入resourc ...

  8. Nodejs_day03

    1.Stream (流) Stream有四种流类型 1.Readable - 可读操作 2.Writable - 可写操作 3.Duplex - 可读可写操作 4.Transform - 操作被写入数 ...

  9. Spring依赖注入 --- 模拟实现

    Spring依赖注入 --- 模拟实现 面向接口编程,又称面向抽象编程, 数据库如果发生更改,对应的数据访问层也应该改变多写几个实现,需要用谁的时候在service里new谁就可以了面向抽象编程的好处 ...

  10. mybatis源码学习: 动态代理的应用(慢慢改)

    动态代理概述 在学spring的时候知道使用动态代理实现aop,入门的列子:需要计算所有方法的调用时间.可以每个方法开始和结束都获取当前时间咋办呢.类似这样: long current=system. ...