LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)
链接:
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(唯一分解定理)的更多相关文章
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- LightOJ-1236 Pairs Forming LCM 唯一分解定理
题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围n<= ...
- 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) == ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- LightOJ 1236 Pairs Forming LCM【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...
- LightOJ 1236 Pairs Forming LCM 合数分解
题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...
- 1236 - Pairs Forming LCM
1236 - Pairs Forming LCM Find the result of the following code: long long pairsFormLCM( int n ) { ...
- Light oj 1236 - Pairs Forming LCM (约数的状压思想)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...
随机推荐
- STM32F030-UART1_DMA使用提示
STM32F030-UART1_DMA使用提示 前言: 今天把STM32F030C8T6的串口DMA学习了一下,为了加快各位研发人员的开发进度,避免浪费大量的时间在硬件平台上,写出个人代码调试的经验. ...
- linux安装go开发环境
1下载go wget https://studygolang.com/dl/golang/go1.12.7.linux-amd64.tar.gz执行此命令会将go1.12.7.linux-amd64. ...
- 【C语言】了解原码、反码、补码
原码.反码.补码 在学习C语言的过程中,有遇到补码这个问题,当时感觉懂了,有貌似不是很懂:然后查了一些文档,整理了一番,以后忘记了可以再翻开这篇文档,查漏补缺吧! 原码 原码是指一个二进制数左边加上符 ...
- javascript 同源策略和 JSONP 的工作原理
同源策略 同源策略是一个约定,该约定阻止当前脚本获取或操作另一域的内容.同源是指:域名.协议.端口号都相同. 简单地说,A 服务器下的 a 端口执行 ajax 程序,不能获取 B 服务器或者 A 服务 ...
- 温故知新-网络基础1-ip地址计算
网络地址换算 1.网络地址分类 A类:0.0.0.0到127.255.255.255 255.0.0.0/8 B类:128.0.0.0到191.255.255.255 255.255.0.0/16 C ...
- 保研经验帖----江西师范大学 to 华中科技大学
呼,距离拿到华科cs的offer也有一段时间,有好几次准备动手写这篇经验帖,但就是理不清自己想表达什么,今早起来状态还不错,洗漱的时候思路居然通透了,哈哈哈~ 一.基本情况 先简单介绍笔者的一些基本情 ...
- Windows中的消息与消息队列
消息 在Windows中,消自由MSG结构体表示 typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lPar ...
- 禁止迅雷极速版被强制升级为迅雷x
PS:迅雷极速版( ThunderSpeed1.0.34.360 )下载地址: https://pan.baidu.com/s/1wuBOpNbim5jBru03AfSAVg 按照下面的这个路径去找. ...
- iOS 打包静态库.a文件(真机版 + 虚拟机)
我们以打包IOS开发中封装的高地地图基础功能包 GDMap为例. 1>我们需要准备好需要打包的GDMap 2>接下来我们开始新建一个工程文件取名GDMap 3>删掉无 ...
- 第三章:JavaScript选择元素
我们使用jQuery时,很常用的套路是“两步”第一步:选取元素第二步:对选中的元素执行需要的操作这一章我们重点研究第一步,如何使用jQuery选取元素以及对选取的结果进行“各种筛选”以满足我们的需求. ...