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 ...
随机推荐
- Android 代码集装箱
1. 一个APP下载升级的Demo(通知栏实时更新下载进度)------(一) 2.一个APP下载升级的Demo(通知栏实时更新下载进度)------(二) 3.APK包名修改
- 多线程简单案例 - join( ) -lock()
join() 在调用结束前,主线程不会结束 不加的话,主线程会在子线程结束前继续执行:加了join(),主线程会等待子线程结束后在继续执行下去 #python3 #main print number ...
- 使用Docker遇到的基本命令及问题小结
当遇到Cannot connect to the Docker daemon. Is the docker daemon running on this host?导致Docker无法启动时,重启Do ...
- lwip BUG ,导致 系统 死机
pcb->snd_queuelen >= pbuf_clen(next->p) sys_arch_assert: in ..\..\User\lwip\src\core\tcp_in ...
- SharePoint客户端对象模型—任务日历生成
1,憋了好几天在经理帮助下用Js根据任务列表,生成的个人任务日历. (1)需要用到的CSS样式 <style type="text/css"> th.ms-vh { c ...
- 【Dubbo源码阅读系列】服务暴露之本地暴露
在上一篇文章中我们介绍 Dubbo 自定义标签解析相关内容,其中我们自定义的 XML 标签 <dubbo:service /> 会被解析为 ServiceBean 对象(传送门:Dubbo ...
- DataGuard的三种保护模式
(一)三种保护模式介绍1.最大性能模式这种模式保证数据库主库性能最大化,主备库之间数据是异步传输的.即,主备日志归档以后才会传输到备库,在备库上使用归档日志文件做恢复操作.这种模式提供在不影响prim ...
- BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 439 Solved: 379[Submit][Status][Discuss] Descriptio ...
- attr 和 prop的区别
attr 返回的是字符串 prop 返回的是布尔值
- TP5.0中多图上传文件名重复问题
最近在做项目的时候出现了一个问题,这里记录一下: 问题: 使用TP5.0框架自带的文件上传方法后,发现多图上传可能会出现文件名重复的问题. 问题代码: 找到TP5框架上传文件命名方法,/thinkph ...