1236 - Pairs Forming LCM
 

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)'

分析:题意1到n中存在多少对(a,b)满足lcm(a, b)==n。

n 是a, b的所有素因子取在a, b中较大指数的积。
将n进行素因子分解 n=a1^p1*a2^p2*...*am^pm(a1,a2,...,am均为质数),先不考虑a, b的大小,如果在a中因子a1取p1个,那么在b中a1可以取(pi-1,pi-2,...,0)个,反之一样,再加上a1在a,b中都是取p1个,所以每个素因子对应2*pi+1中情况。所以总数为 sigma(2*pi+1)/2。
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>

using namespace std;
typedef long long ll;
const int maxn = 1e7+5;

int prim[maxn/10] ;
int k;
int vis[maxn];

void init() //线性筛
{
k = 0;
for(int i = 2; i < maxn; ++i)
{
if(!vis[i]) prim[k++] = i;
for(int j = 0; j < k && prim[j] * i < maxn; ++j)
{
vis[prim[j] * i] = 1;
if(i % prim[j] == 0) break;
}
}
}
int main(void)
{
int T, cas;
ll n;

scanf("%d", &T);

cas = 0;
init();

while(T--)
{
cas++;

scanf("%lld", &n);
ll ans = 1;

for(int i = 0; i < k; i++)
{
ll x = 0;

if((ll)(prim[i]) * prim[i] > n)
break;

while(n % prim[i] == 0)
{
x++;
n /= prim[i];
}
if(x)
ans *= 2 * x + 1;
}
if(n > 1)///n>1表示最后还剩一个素因子,比如6,20,并且这个素因子的平方大于n,再循环中没有处理。剩余最后一个因子按分析中的方法它对应三种取法,所以最后乘在ans上。
ans *= 3;

printf("Case %d: %lld\n", cas, ans / 2 + 1);

}

return 0;

}

 

1236 - Pairs Forming LCM的更多相关文章

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

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

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

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

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

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

  4. Light oj 1236 - Pairs Forming LCM (约数的状压思想)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...

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

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

  6. 1236 - Pairs Forming LCM -- LightOj1236 (LCM)

    http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...

  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 (素数筛选&&唯一分解定理)

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

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

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

随机推荐

  1. __init__.py在导包中起到的作用

    说明:__init__.py这个文件本人使用频率不高 例子: __init__.py文件作用:初始化这个包 1.v1.py def vi(): return 'views' 2.views->_ ...

  2. RocketMQ 实战之快速入门

    原文地址:https://www.jianshu.com/p/824066d70da8 最近 RocketMQ 刚刚上生产环境,闲暇之时在这里做一些分享,主要目的是让初学者能快速上手RocketMQ. ...

  3. 实验四:划分多个VLAN

    1.配置图 2.配置命令 Switch1.Switch2.Switch3的配置是一样的,如下所示:(可直接复制交换机,可以只配置一次) 通过命令查看配置: Switch0的配置如下: 通过命令查看tr ...

  4. 并查集——奇偶性(Parity)

    题目描述 •有一个01序列,长度<=1000000000,现在有n条信息,每条信息的形式是-a b even/odd.表示第a位到第b位元素之间的元素总和是偶数/奇数. •你的任务是对于这些给定 ...

  5. Ubuntu16手动安装OpenStack

    记录大佬的博客全文转载于https://www.voidking.com/dev-ubuntu16-manual-openstack-env/ 前言 <Ubuntu16安装OpenStack&g ...

  6. 软工造梦厂团队项目(Alpha版本发布2)

    课程 (https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience) 作业要求 https://www.cnblogs.com/h ...

  7. springboot中的那些连接池

    hello~各位读者新年好! 回想起前几天在部署springboot项目到正线时,线上环境要求jdk7,可项目是基于jdk8开发的,springboot也是用的springboot2以上的版本,可以说 ...

  8. [HAOI2015]树上操作(树链剖分)

    [HAOI2015]树上操作(luogu) Description 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种: 操作 1 :把某个节点 x 的点权增 ...

  9. CTF--HTTP服务--SSI注入

    开门见山 1. 扫描靶场ip,发现VM 192.168.31.160 2. 扫描主机服务信息和服务版本 3. 快速扫描靶场全部信息 4. 探测开放的http的敏感信息 5. 再用dirb扫描敏感页面 ...

  10. [Java] The imoprt XXX cannot be resolved

    不知道为什么突然报错,但是可以成功编译. 通过import一个未使用的包来消除error,猜测是插件的问题,具体原因未知. import 未使用的包后,error消失.