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

转https://www.cnblogs.com/shentr/p/5285407.html

先来看个知识点:

素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

for i in range(,n):

        ei 从0取到ei的所有组合

必能包含所有n的因子。

现在取n的两个因子a,b

a=p1 ^ a1 * p2 ^ a2 *..........*pn ^ an

b=p1 ^ b1 * p2 ^ b2 *..........*pn ^ bn

gcd(a,b)=p1 ^ min(a1,b1) * p2 ^ min(a2,b2) *..........*pn ^ min(an,bn)

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

哈哈,又多了种求gcd,lcm的方法。

题解:

先对n素因子分解,n = p1 ^ e1 * p2 ^ e2 *..........*pk ^ ek,

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pk ^ max(ak,bk)

所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek

当ai == ei时,bi可取 [, ei] 中的所有数  有 ei+ 种情况,bi==ei时同理。

那么就有2(ei+)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+)-=*ei+。
除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有 (*ei + )) / + 个 复制代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e7+;
const int NN=1e6;
unsigned int prime[NN],cnt; //prime[N]会MLE
bool vis[N]; void is_prime()
{
cnt=;
memset(vis,,sizeof(vis));
for(int i=;i<N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i+i;j<N;j+=i)
{
vis[j]=;
}
}
}
} int main()
{
is_prime();
int t;
cin>>t;
for(int kase=;kase<=t;kase++)
{
LL n;
cin>>n;
int ans=;
for(int i=;i<cnt&&prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
int e=;
while(n%prime[i]==)
{
n/=prime[i];
e++;
}
ans*=(*e+);
}
}
if(n>)
ans*=(*+);
printf("Case %d: %d\n",kase,(ans+)/);
}
}
复制代码

Pairs Forming LCM LightOJ - 1236 素因子分解的更多相关文章

  1. G - Pairs Forming LCM LightOJ - 1236 (质因子分解)

    题解:这道题要从n的角度来考虑i和j. n可以表示为n=a1^p1*a2^p2*a3^p3.......n=lcm(i,j),那么质因子a1^p1,a1可以在i或者j中,并且p1=max(a1i,a1 ...

  2. Pairs Forming LCM LightOJ - 1236 (算术基本定理)

    题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n  (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...

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

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

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

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

  5. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  6. 1236 - Pairs Forming LCM

    1236 - Pairs Forming LCM   Find the result of the following code: long long pairsFormLCM( int n ) {  ...

  7. Pairs Forming LCM(素因子分解)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B    全题在文末. 题意:在a,b中(a,b<=n) ...

  8. Pairs Forming LCM

    题目: B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB Description Find the result of ...

  9. Pairs Forming LCM (LCM+ 唯一分解定理)题解

    Pairs Forming LCM Find the result of the following code: ; i <= n; i++ )        for( int j = i; j ...

随机推荐

  1. EF Core 2.1 Raw SQL Queries (转自MSDN)

    Entity Framework Core allows you to drop down to raw SQL queries when working with a relational data ...

  2. SSM衍生的配置文件

    JDBC:(Java database connectivity) 目的:将Java语言和数据库解耦和,使得一套Java程序能对应不同的数据库. 方法:sun公司制定了一套连接数据库的接口(API). ...

  3. Git IDEA Move or commit them before merge

    提交代码遇到这个问题. Move or commit them before merge 百度了一下都是在Gitbash 中敲命令. 在团队协作中 你总不能去敲命令吧 后来在组长的怂恿下,我删除了一个 ...

  4. Python绘制奥运五环

    绘制奥运五环主要涉及到Python中的turtle绘图库运用: turtle.forward(distance) 向当前画笔方向移动distance像素长度 turtle.backward(dista ...

  5. thinkphp5.1 学习笔记 【多态关联】

    $result = Draft::update($input, ['id' => $input['id']], true); if (!empty(array_get($input, 'hous ...

  6. 基于 HTML5 Canvas 的拓扑组件开发

    在现在前端圈大行其道的 React 和 Vue 中,可复用的组件可能是他们大受欢迎的原因之一, 在 HT 的产品中也有组件的概念,不过在 HT 中组件的开发是依托于 HTML5 Canvas 的技术去 ...

  7. 运维与架构—Nginx的优缺点

    Nginx的优点是: 1.工作在网络的7层之上,可以针对http应用做一些分流的策略,比如针对域名.目录结构 它的正则规则比HAProxy更为强大和灵活,这也是它目前广泛流行的主要原因之一 Nginx ...

  8. python 运算符与分支结构

    运算符与分支结构 运算符 赋值运算符 用'='表示,左边只能是变量 算术运算符 +.-.*:加.减.乘 /:除法运算,结果是浮点型 //:除法运算,结果是整型 %:求余 **:求幂 复合运算符 +=. ...

  9. TCP/IP协议的数据传输过程详解——IP与以太网的包收发操作

    MTU:一个网络包的最大长度,以太网中一般是1500字节:(含有头部长度,包括IP头部,TCP头部,不包括MAC头部) MSS:除去头部后,一个网络包所能容纳的TCP的数据的最大长度 下图为TCP/I ...

  10. 第六节 Go数据结构之集合

    一.什么是集合 集合就是不同对象的无序聚集.那么链表和集合有什么关系呢?我们来变个魔术.如下图是各种颜色组成的链表: 下面我们一步步把链表变成集合. 第一步砍去链接 第二步去掉重复 第三步放到一个框里 ...