LightOJ1236 —— 唯一分解定理 + 最小公倍数
题目链接:https://vjudge.net/problem/LightOJ-1236
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
15 2 3 4 6 8 10 12 15 18 20 21 24 25 27 29 |
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 |
题意:
求1到n(1e14)之内,有多少对数(i,j),其中i<=j,使得LCM(i,j)= n,LCM为最小公倍数。
题解:
1.设pi为第i个质数。设两个数A、B,他们可表示为:A = p1^a1 * p2^a2…… ,B = p1^b1 * p2^b2……。
那么他们的最小公倍数为:LCM(A, B) = p1^max(a1,b1) * p2^max(a2, b2)……。
2.对n进行质因数分解,得到: n = p1^c1 * p2^c2……。当 LCM(A, B) = n时, ci = max(ai, bi),即要么 ci = ai,要么ci = bi。
3 当ci = ai时, bi的可选择范围为[0,ci]共ci+1种;同理当ci = bi时,ai也有ci+1种选择。但是 (ai=ci,bi=ci)被重复计算了一次,所以对于素数pi,总共有 2*ci+1种选择。所以,当不考虑A、B的大小时,总共有 ∏ 2*ci+1对(A,B),使得 LCM(A, B) = n。
4.再考虑回A、B的大小限制,即A<=B,可知除了A = B = n时,其他的组合都出现了两次,即(A,B)和(B,A)都存在,而要门只需要A<=B的那一个。总的来说,最终有 ((∏ 2*ci+1)+1)/2对 (A,B)满足条件。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e7+; bool notprime[MAXN];
int prime[MAXN/];
void getPrime()
{
memset(notprime, false, sizeof(notprime));
notprime[] = notprime[] = true;
prime[] = ;
for (int i = ; i<MAXN; i++)
{
if (!notprime[i])prime[++prime[]] = i;
for (int j = ; j<=prime[]&& prime[j]<MAXN/i; j++)
{
notprime[prime[j]*i] = true;
if (i%prime[j] == ) break;
}
}
} int fatCnt;
LL factor[][];
int getFactors(LL n)
{
LL tmp = n;
fatCnt = ;
for(int i = ; prime[i]<=tmp/prime[i]; i++)
{
if(tmp%prime[i]==)
{
factor[++fatCnt][] = prime[i];
factor[fatCnt][] = ;
while(tmp%prime[i]==) tmp /= prime[i], factor[fatCnt][]++;
}
}
if(tmp>) factor[++fatCnt][] = tmp, factor[fatCnt][] = ;
} int main()
{
getPrime();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
LL n;
scanf("%lld", &n);
getFactors(n);
LL sum = ;
for(int i = ; i<=fatCnt; i++)
sum *= *factor[i][]+; sum = (sum+)/;
printf("Case %d: %lld\n", ++kase, sum);
}
}
LightOJ1236 —— 唯一分解定理 + 最小公倍数的更多相关文章
- Uva 10791 最小公倍数的最小和 唯一分解定理
题目链接:https://vjudge.net/contest/156903#problem/C 题意:给一个数 n ,求至少 2个正整数,使得他们的最小公倍数为 n ,而且这些数之和最小. 分析: ...
- LightOJ-1236 Pairs Forming LCM 唯一分解定理
题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围n<= ...
- NOIP2009Hankson 的趣味题[唯一分解定理|暴力]
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- UVa 10791 Minimum Sum LCM【唯一分解定理】
题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...
- 唯一分解定理(以Minimun Sum LCM UVa 10791为例)
唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...
- hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)
GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- Uva10791 唯一分解定理模板
唯一分解定理: Uva10791 题意: 输入整数n,要求至少两个正整数,使得他们的最小公倍数为n,且这些整数的和最小 解法: 首先假设我们知道了一系列数字a1,a2,a3……an,他们的LCM是n, ...
- UVA10791-Minimum Sum LCM(唯一分解定理基本应用)
原题:https://vjudge.net/problem/UVA-10791 基本思路:1.借助唯一分解定理分解数据.2.求和输出 知识点:1.筛法得素数 2.唯一分解定理模板代码 3.数论分析-唯 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- 浅谈APP的分享功能,有时候社交裂变形式比内容更“重要”
回顾2018年的移动互联网,“社交裂变”“下沉”等成为年度关键词.一方面我们可以看到社交裂变助推用户增长,另一方面我们也看到了以拼多多.趣头条为代表的互联网企业对于社交裂变模式表现出的空前关注度.作为 ...
- rostopic pub
rostopic pub -1 reinit_motor_wheel std_msgs/String -- "reinit_motor_wheel"rostopic pub -r ...
- BZOJ1006神奇的国度 弦圖染色 最大勢算法
@[弦圖染色, 最大勢算法] Description K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA 相互认识,是简洁高效的.为了巩固三角 ...
- 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面
贼烦的是,使用spring boot 创建web项目,然后我再idea下创建的,but 仅仅启动spring boot的启动类,就算整个项目都是好着的,就算是能够进入controller中,也不能成功 ...
- 精简版的MySQL制作步骤
1.删除所有的目录,只保留 datasharebin 2.删除BIN下面除以下三个文件之外的所有文件: libmysql.dll(MYSQL5中的文件,在MYSQL5.5中不存在)mysqladmin ...
- iOS开发 Coretext基本用法
转至 http://blog.csdn.net/fengsh998/article/details/8691823 API接口文档. https://developer.apple.com/libra ...
- C++ 用libcurl库进行http通讯网络编程 【转】
http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html C++ 用libcurl库进行http通讯网络编程 目录索引: 一.Lib ...
- C#中异常处理和Java的区别
捕获异常,同样是try...catch...,这个完全一样: 抛出异常,同样是throw,这个完全一样: 函数抛出怎样的异常,Java中可以用throws定义,而C#中不用定义,相当于throws E ...
- eclipse中文凝视字体太小解决方法
新安装的eclipse中文凝视字体太小.解决方法例如以下: 打开Elcipse-->点击菜单条上的"Windows"-->点击"Preferences&quo ...
- django_session
基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie session原理 cookie是保存在用户浏览器端的键值对 session是保存在服务器端的键值对 s ...