Pairs Forming LCM 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).
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 素因子分解的更多相关文章
- 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 ...
- Pairs Forming LCM LightOJ - 1236 (算术基本定理)
题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...
- 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 (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- 1236 - Pairs Forming LCM
1236 - Pairs Forming LCM Find the result of the following code: long long pairsFormLCM( int n ) { ...
- Pairs Forming LCM(素因子分解)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B 全题在文末. 题意:在a,b中(a,b<=n) ...
- Pairs Forming LCM
题目: B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB Description Find the result of ...
- Pairs Forming LCM (LCM+ 唯一分解定理)题解
Pairs Forming LCM Find the result of the following code: ; i <= n; i++ ) for( int j = i; j ...
随机推荐
- ansible copy 模块 changed false 没有变化
在分发配置文件的时候,我用命令ansible big_hosthub -m copy -a "src=/home/clouder/deploy-conf.xml dest=/home/cl ...
- CC2640R2F&TI-RTOS 拿到 TI CC2640R2F 开发板 第一件事就是移植串口驱动,重定向 printf
/* * board_uart.c * * Created on: 2018年7月3日 * Author: admin */ #include "board_uart.h" #in ...
- iOS:时间相关(18-10-13更)
先整理出时间相关的程序,以后有空再写成单例. 1.日历(NSCalendar) 2.时间格式() 3.时间戳 附录: 1.定时器 1.日历(NSCalendar) 1.想要获取 世纪.年.月.日.时. ...
- linux查看网卡地址和硬盘序列号
linux查看网卡地址命令:ifconfig linux查看硬盘序列号命令:hdparm -i /dev/sda
- jquery选择器基础
简单选择器 类 id 元素/标签 * 复合(sel1,sel2)逗号隔开 层次选择器 s1 s2:后代选择器,空格隔开 p>c:子代选择器:不包括孙代及以下 p+next :相邻选择器 p~su ...
- c# 在 websocket 中访问 session
今天的项目需要用到websocket,但是中websocket中无法直接访问session,一访问session就出错,断开连接. 找了老半天百度也没有相关c#的介绍和方法,没办法,找不到现成的代码, ...
- PHpStorm + phpstudy 断点调试配置
步骤: phpstudy 其他菜单 ->打开配置文件 ->打开 php.ini ->找到 XDebug ->XDebug修改成下面文字 [XDebug] :红色 (1 ...
- 一次JVM调优经历
前几天前端同事找我帮忙解决一个频繁FullGC问题.在100用户,每秒5个请求条件下进行测试,发现频繁FullGC. 使用VisualVM观察Jvm运行时信息,发现DB连接池占用了较多的资源.于是看了 ...
- S2-057远程代码执行漏洞复现过程
0x01 搭建环境docker https://github.com/vulhub/vulhub/tree/master/struts2/s2-048 docker-compose up -d 0x0 ...
- dns欺骗之ettercap
ettercap是一个基于ARP地址欺骗方式的网络嗅探工具,主要适用于局域网. ettercap是一款现有流行的网络抓包软件,它利用计算机在局域网内进行通信的ARP协议的缺陷进行攻击,在目标与服务器之 ...