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. crawler 听课笔记 碎碎念 2 一些爬虫须知的基本常识和流程

    html的宗旨:      <标签 属性=”属性的值“></标签>        只是对于文本的一种解释划分吧 dom的宗旨:      就是一个大数组,处理方便,效率低 xm ...

  2. css 脱离文档流

    一.float <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&quo ...

  3. Java入门 - 语言基础 - 16.数组

    原文地址:http://www.work100.net/training/java-array.html 更多教程:光束云 - 免费课程 数组 序号 文内章节 视频 1 概述 2 声明数组变量 3 创 ...

  4. python property()函数:定义属性

    正常情况下,类包含的属性应该是隐藏的,只允许通过类提供的方法来间接的实现对类属性的访问和操作. class Person: #构造函数 def __init__(self, name): self.n ...

  5. 【WPF学习】第十章 WPF布局示例

    前几章用了相当大的篇幅研究有关WPF布局容器的复杂内容.在掌握了这些基础知识后,就可以研究几个完整的布局示例.通过研究完整的布局示例,可更好的理解各种WPF布局概念在实际窗口中的工作方式. 一.列设置 ...

  6. 【模板整理】Tarjan

    有向图强连通分量 int tot,low[N],dfn[N],scc[N],sccno; int st[N],top,vis[N]; void tarjan(int u){ int v; low[u] ...

  7. Idea破解至2089年

    我是用的版本是2018.3.6,别的朋友使用的是2019的某个版本,不过关都不影响破解 下载jar包:链接:https://pan.***baidu.***com/s/1aRR0***2YNI9jew ...

  8. Python3基础之数据类型(字典)

    Python3数据类型之 字典 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({} ...

  9. python文件、文件夹的相关操作

    python文件.文件夹的相关操作 #1.rename()可以完成对文件的重命名 #rename(需要修改的文件名,新的文件名) import os os.rename("readme.tx ...

  10. IIS发布网站常见MIME扩展类型添加

    # This file maps Internet media types to unique file extension(s). # Although created for httpd, thi ...