链接:

https://vjudge.net/problem/LightOJ-1236

题意:

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

思路:

考虑lcm(a, b)= n,有\(a = p_1^{k1}*p_2^{k2}*p_3^{k3}, b = p_1^{t1}*p_2^{t2}, n = p_1^{e1}*p_2^{e2}\).

如果想lcm(a, b) = n,得保证对于每个p,max(ki, ti) = ei.保证每个素数满足n的指数。

这样每个p有(2*ei+1)种取值,减去相同。

则得到了无序对(a, b)

有序对则加1除2,因为相同的只计算了一次。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map> using namespace std;
typedef long long LL;
const int INF = 1e9; const int MAXN = 1e7+10;
const int MOD = 1e9+7; bool IsPrime[MAXN];
int Prime[1000010];
int tot;
LL n; void Init()
{
tot = 0;
memset(IsPrime, 0, sizeof(IsPrime));
IsPrime[1] = 1;
for (int i = 2;i < MAXN;i++)
{
if (IsPrime[i] == 0)
Prime[++tot] = i;
for (int j = 1;j <= tot && i*Prime[j] < MAXN;j++)
{
IsPrime[i*Prime[j]] = 1;
if (i%Prime[j] == 0)
break;
}
}
} int main()
{
Init();
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%lld", &n);
LL x = n;
LL sum = 1;
for (int i = 1;i <= tot && Prime[i] <= x;i++)
{
if (x%Prime[i] == 0)
{
int cnt = 0;
while(x%Prime[i] == 0)
{
cnt++;
x /= Prime[i];
}
sum *= (2LL*cnt+1);
}
}
if (x>1)
sum *= 3LL;
sum = (sum+1)/2;
printf(" %lld\n", sum);
} return 0;
}

LightOJ - 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 唯一分解定理

    题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围n<= ...

  4. 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) == ...

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

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

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

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

  7. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  8. 1236 - Pairs Forming LCM

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

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

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

随机推荐

  1. Python规范:代码规范要注意

    主要有以下两种代码规范 <8 号 Python 增强规范>(Python Enhacement Proposal #8),以下简称 PEP8: <Google Python 风格规范 ...

  2. Qt 中的二进制兼容策略(简而言之就是地址不能变,剩下的就是让地址不变的技巧)

    本文翻译自 Policies/Binary Compatibility Issues With C++ 二进制兼容的定义 如果程序从一个以前版本的库动态链接到新版本的库之后,能够继续正常运行,而不需要 ...

  3. 服务端技术选型与考虑(go)

  4. 二进制方式安装Kubernetes 1.14.2高可用详细步骤

    00.组件版本和配置策略 组件版本 Kubernetes 1.14.2 Docker 18.09.6-ce Etcd 3.3.13 Flanneld 0.11.0 插件: Coredns Dashbo ...

  5. .NET/C# 检测电脑上安装的 .NET Framework 的版本

    原文:.NET/C# 检测电脑上安装的 .NET Framework 的版本 如果你希望知道某台计算机上安装了哪些版本的 .NET Framework,那么正好本文可以帮助你解决问题. 本文内容 如何 ...

  6. Python3 压缩与解压缩(zlib / gzip / bz2 / lzma / zipfile / tarfile)

    本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/72672703 以下代码以Python3.6.1为例 L ...

  7. VBA 字符串-相关函数(6-12)

    Mid()函数 Mid()函数返回给定输入字符串中指定数量的字符. 语法 Mid(String,start[,Length]) 参数 String - 必需的参数.输入从中返回指定数量的字符的字符串. ...

  8. 【转载】 C#使用string.IsNullOrWhiteSpace方法判断字符串是否为非空字符

    在C#编程过程中,很多时候需要判断传入过来的字符串是否为Null或者空字符或者空白字符,此时就可以使用到string.IsNullOrWhiteSpace方法来判断,如果字符串为null或者空字符Em ...

  9. 重置文件reset

    body { margin:0; padding:0; font-family: Helvetica, STHeiti, Droid Sans Fallback; // font-family: '微 ...

  10. JavaScript之条件语句

    (1)if条件语句 // if(条件){当条件为真,存在即为真.当条件为false null 0 undefined中任意一种时,则表示不存在,不存在即为假} if(条件){ 条件为真时执行的代码 } ...