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 ...
随机推荐
- 精读 SBAR SDN flow-Based monitoring and Application Recognition
目录 架构 测量模块 分类模块 具体实现 实验:最后接入巴塞罗那的校园网流量测试: SBAR: SDN flow-Based monitoring and Application Recognitio ...
- DPDK+Pktgen 高速发包测试
参考博客 Pktgen概述 Pktgen,(Packet Gen-erator)是一个基于DPDK的软件框架,发包速率可达线速. 提供运行时管理,端口实时测量. 可以控制 UDP, TCP, ARP, ...
- VPP(Vector Packet Processing)浅析
VPP简介 VPP(Vector Packet Processing)是思科旗下的一款可拓展的开源框架,提供容易使用的.高质量的交换.路由功能 特点:高性能.运行在普通的cpu上. 优点:高性能.技术 ...
- mysql 全量备份与增量备份
全量备份[root@master adm]# cat DBfullBak.sh #!/bin/bash #use mysqldump to fully backup mysql data BakDir ...
- Redhat7.2 ----team网卡绑定
我先声明一下,team和bonding是一样的作用,只不过team多了几项功能bonding没有, 做team我们要最少准备两个网卡,我们这里主要显示主备模式. 首先我们先把网卡配置文件删除 nmcl ...
- GPUImage源码解读之GLProgram
简述 GLProgram是GPUImage中代表openGL ES 中的program,具有glprogram功能.其实是作者对OpenGL ES program的面向对象封装 初始化 - (id)i ...
- SQL循环插入批量数据
declare @i intdeclare @qid int set @i=1set @qid=100 while @i<50000begininsert into Order(orderid, ...
- HCDA day1
OSI有几层: OSI将计算机网络体系结构(architecture)划分为以下七层: 图1.OSI模型 物理层: 将数据转换为可通过物理介质传送的电子信号 相当于邮局中的搬运工人. 物理层(Phys ...
- ssm多数据源的操作
公司要求,需要使用两个数据库,一个mysql,一个oracle.所以需要配置两个数据库来进行操作. 1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver, ...
- mvc.net路由中带特殊字符如【.*/\】等时遇到的天坑
用mvc.net的路由做网站伪静态时出现的天坑,自己一直没测试出来,竟然要靠客户被坑了后才知道 解决办法 参考https://stackoverflow.com/questions/16581184/ ...