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 ...
随机推荐
- [BJWC2018]最长上升子序列
十分感谢GXZ大佬的讲解,此处致以敬意!emmmm在初学状压DP时就理解了如此精妙的一道题,感到很开森~ \(Address\) ________________ #\(\color{red}{\ma ...
- EF Core 中DbContext不会跟踪聚合方法和Join方法返回的结果,及FromSql方法使用讲解
EF Core中: 如果调用Queryable.Count等聚合方法,不会导致DbContext跟踪(track)任何实体. 此外调用Queryable.Join方法返回的匿名类型也不会被DbCont ...
- nodejs运行的时候报错:Error: write EIO以及乱码解决方式
在运行node.js的过程中报如下错误: events.js:72 throw er; // Unhandled 'error' event ^ Error: write EIO at errnoEx ...
- 运行TensorFlow报错,“This program requires version 3.6.1 of the Protocol Buffer runtime library, but the installed version is 3.0.0.”
报错信息: [libprotobuf FATAL google/protobuf/src/google/protobuf/stubs/common.cc:67] This program requir ...
- 大数据学习--day14(String--StringBuffer--StringBuilder 源码分析、性能比较)
String--StringBuffer--StringBuilder 源码分析.性能比较 站在优秀博客的肩上看问题:https://www.cnblogs.com/dolphin0520/p/377 ...
- else的妙用--if-else 成对出现,可以检测意外的放生,方便找bug
else用于意外情况的检测.可以写入log,或者串口打印出来. 养成这样的习惯,好处 1 提高程序稳定性,可靠性. 2 遇到bug有头绪.
- Zeta--S3 Linux抓取一帧YUV图像后使用硬件编码器编码成H.264
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h&g ...
- 编写DVWA暴力破解High级别的Python脚本
1. 过程(不查看源代码) 使用burpsuite或者owasp zap抓取数据包,可以看出页面执行过程如下: 首先获取上一次请求的token,然后sleep几秒,最后使用get方法提交验证. 2. ...
- A1048
给n个正整数,再给一个m,如果n个数中存在a+b=m(a<=b),则输出a,b. 如果有两组以上,则输出a最小的一组. #include<cstdio> #include<al ...
- python 3下对stm32串口数据做解析
1.最近有个想做一个传感器数据实时显示的上位机,常规的数据打印太频繁了,无法直观的看出数据的变化. python下的上位机实现起来简单一点,网上找了一些python界面Tkinter相关资料和pyth ...