Pairs Forming LCM (LCM+ 唯一分解定理)题解
Pairs Forming LCM
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
25
27
29
Sample Output
Case 1: 2
Case 2: 2
Case 3: 3
Case 4: 5
Case 5: 4
Case 6: 5
Case 7: 8
Case 8: 5
Case 9: 8
Case 10: 8
Case 11: 5
Case 12: 11
Case 13: 3
Case 14: 4
Case 15: 2
题意:
在a,b中(a,b<=n)(1 ≤ n ≤ 1014),有多少组(a,b) (a<b)满足lcm(a,b)==n;
思路:
这里要学个新东西:快问问神奇海螺
首先,我们已经知道了唯一分解定理:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en
那么,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)
现在我们就可以求解题目了。
要a,b的LCM是n,所以max(a1,b1)== e1,max(a2,b2)== e2以此类推到n,也就是说每一个ai,bi中至少有一个等于ei,求这种组合方式有多少。按上面的思路第pi组有2*(ei+1)种组合方式,但是还要再减去一种重复的 ai==bi==ei ,所以结果是 2*ei+1 种。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=1e7+5;
const int NN=1e6;
const int MOD=1000;
using namespace std;
bool prime[N];
ll p[NN]; //这里只能1e6不然就MLE了
int pn;
void get_prime(){
pn=0;
memset(prime,false,sizeof(prime));
prime[0]=prime[1]=true;
for(ll i=2;i<N;i++){
if(!prime[i]){
p[pn++]=i;
for(ll j=i*i;j<N;j+=i){
prime[j]=true;
}
}
}
}
ll deal(ll n){
ll res=1;
for(ll i=0;i<pn && p[i]*p[i]<=n;i++){
if(n%p[i]==0){
int tmp=0;
while(n%p[i]==0){
tmp++;
n/=p[i];
}
res*=(2*tmp+1);
}
}
if(n>1) res*=3;
res=res/2+1;
return res;
}
int main(){
get_prime();
int T,num=1;
ll ans,n;
scanf("%d",&T);
while(T--){
scanf("%lld",&n);
ans=deal(n);
printf("Case %d: %lld\n",num++,ans);
}
return 0;
}
Pairs Forming LCM (LCM+ 唯一分解定理)题解的更多相关文章
- UVa 10791 Minimum Sum LCM【唯一分解定理】
题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...
- UVa 10791 - Minimum Sum LCM(唯一分解定理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 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 唯一分解定理
题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围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 (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- UVA.10791 Minimum Sum LCM (唯一分解定理)
UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...
- Pairs Forming LCM LightOJ - 1236 素因子分解
Find the result of the following code: long long pairsFormLCM( int n ) { long long res = 0; fo ...
随机推荐
- 用Monitor简单3步监控中间件ActiveMQ
Apache ActiveMQ是一个基于JMX规范的纯Java消息中间件,它为应用系统提供高效.灵活的消息同步与异步传输处理.存储转发.可靠传输的特性. 消息队列对于应用的健康运行非常重要,作为运维人 ...
- 【解决】win10 启用系统保护 灰色 不可选 的解决办法
安装Win10后,发现系统保护无法启用.'启用系统保护' 灰色 不可选状态: 搜了一堆解决办法,包括: 1.组策略修改 2.服务启用 3.各种重启 等,不适用. 然后,我把系统预留分区 删了,删了…… ...
- (3.3)mysql基础深入——mysql启动深入分析
基础:(2.1)学习笔记之mysql基本操作(启动与关闭) 0.mysql启动的 3种方式 (1)mysql.server (2)mysqld_safe (3)mysqld 1.启动分析 [1.1]概 ...
- mysql 操作sql语句 操作数据表中的内容/记录
#3. 操作文件中的内容/记录 往哪张表去插入 insert into 表名指定字段(id,name) 插入要加values(针对前面字段插入)(2,mike); insert into t1(id, ...
- 查看Django和flask版本
查看Django版本 检查是否安装成功,可以在dos下查看Django版本. 1.输入python 2.输入import django 3.输入django.get_version() 1 2 3 4 ...
- kmeans聚类源代码
代码是在weka上二次开发的,但没有使用原来的kmeans代码,只是用了它的数据类Intances,先说下与它相关的几点东西. 一.KMeans算法简介 输入:聚类个数k,以及包含 n个数据对象的数据 ...
- [LeetCode] 827. Making A Large Island
In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...
- XDU1024简单逆序对(贪心||分治)
题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...
- cygwin本地.bashrc配置
echo -e "====================================================================================== ...
- Masonry 适配label多行
设置属性后,然后根据文本自动多行显示,无需设置标签高度约束 1 属性preferredMaxLayoutWidth,如:label.preferredMaxLayoutWidth = (WidthSc ...