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以 ...
随机推荐
- [转帖]【MySQL+keepalived】用keepalived实现MySQL主主模式的高可用
[MySQL+keepalived]用keepalived实现MySQL主主模式的高可用 https://www.jianshu.com/p/8694d07595bc 一.实验说明 MySQL主主模式 ...
- Delphi百度语音【支持语音识别和语音合成】
作者QQ:(648437169) 点击下载➨百度语音 语音识别api文档 语音合成api文档 [Delphi 百度语音]支持获取 Access Token.语音识别.语 ...
- flask框架(七)——蓝图、请求上下文、g对象、信号、flask_session
蓝图 作用:对程序进行目录结构划分 不使用蓝图情况下,自己分文件 目录结构: -templates -views -__init__.py -user.py -order.py -app.py app ...
- tensorflow-简单的神经网络
本次笔记是关于tensorflow1的代码,由于接触不久没有跟上2.0版本,这个代码是通过简单的神经网络做一个非线性回归任务,(如果用GPU版本的话第一次出错就重启) import tensorflo ...
- Spring MVC前端控制器不拦截静态资源配置
- nginx反向代理的一次实践
场景:前端(VUE.js)应用部署在linux服务器,需要支持http和https访问. 问题1:阿里服务器不支持域名访问? 通过域名绑定服务器解决 问题2:如何通过http访问前端 前端通过ngin ...
- Unity3D 跨平台原理
Unity3D的跨平台原理核心在于对指令集CIL(通用中间语言)的应用. 机理 首先需要知道,Unity中的Mono是基于 通用语言架构(Common Language Infrastructure, ...
- Navicat premium工具转储数据表的结构时,datatime字段报错
Navicat premium工具导出数据库: Navicat premium工具导入数据库: 运行SQL文件,遇到的错误,红色下划线提示,发现:(SQL文件的时间有问题) 不是insert语句有问题 ...
- extend Thread 和 implements Runnable
原文地址:extend Thread 和 implements Runnable 一个Thread的实例只能产生一个线程 or: 同一实例(Runnable实例)的多个线程 look: public ...
- SQL Server修改表的模式schema
use myDBgo create schema myschema --先建立go alter schema myschema transfer dbo.myTable --移动对象至建立的schem ...