Pairs Forming LCM
题目:
B - Pairs Forming LCM
Time Limit:2000MS Memory Limit:32768KB
Description
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给定一个数字,n,求1~n之间可以找到的最小公倍数为n的对数
解题思路:
素因子分解:n=p1^x1*p2^x2**************pn^xn;
a=p1^y1*p2^y2*p3^y3*****************pn^yn;
b=p1^c1*p2^c2*p3^c3*****************pn^cn;
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)
所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek
当ai == ei时,bi可取 [0, ei] 中的所有数 有 ei+1 种情况,bi==ei时同理。
那么就有2(ei+1)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+1)-1=2*ei+1。
除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有 (2*ei + 1)) / 2 + 1 个
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <cstdio>
- #include <vector>
- #include <iomanip>
- #include <cmath>
- #include <ctime>
- #include <map>
- #include <set>
- using namespace std;
- #define lowbit(x) (x&(-x))
- #define max(x,y) (x>y?x:y)
- #define min(x,y) (x<y?x:y)
- #define MAX 100000000000000000
- #define MOD 1000000007
- #define pi acos(-1.0)
- #define ei exp(1)
- #define PI 3.141592653589793238462
- #define INF 0x3f3f3f3f3f
- #define mem(a) (memset(a,0,sizeof(a)))
- typedef long long ll;
- const int mx=;
- ll pos[];
- ll top=,n,t,k;
- bool vis[mx];
- void get_prime()
- {
- for(int i=;i<mx;i++)
- {
- if(vis[i]) continue;
- pos[top++]=i;
- for(int j=;j*i<mx;j++)
- {
- vis[i*j]=;
- }
- }
- }
- ll solve(ll n)
- {
- ll ans=;
- for(int i=;i<top && pos[i]*pos[i]<=n;i++)
- {
- if(n%pos[i]==)
- {
- int cnt=;
- while(n%pos[i]==)
- {
- n/=pos[i];
- cnt++;
- }
- ans*=(*cnt+);
- }
- }
- if(n>) ans*=(*+);
- return (ans+)/;
- }
- int main()
- {
- get_prime();
- scanf("%lld",&t);
- k=t;
- while(t--)
- {
- scanf("%lld",&n);
- printf("Case %lld: %lld\n",k-t,solve(n));
- }
- }
Pairs Forming LCM的更多相关文章
- Pairs Forming LCM(素因子分解)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B 全题在文末. 题意:在a,b中(a,b<=n) ...
- 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 (LCM+ 唯一分解定理)题解
Pairs Forming LCM Find the result of the following code: ; i <= n; i++ ) for( int j = i; j ...
- 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 LightOJ - 1236 素因子分解
Find the result of the following code: long long pairsFormLCM( int n ) { long long res = 0; fo ...
- LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)
链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
随机推荐
- 赋值、复制构造函数和构造函数 & 异常安全的赋值
异常安全的赋值 需要注意,复制赋值和复制构造,相兼容. 赋值时候,要带上自检查.
- 《AndroidStudio每日一贴》5. 怎样高速查看某个方法/注解的定义?
操作方法: 使用快捷键 option + space 或 command + y 举个样例: 如以下的样例,我在输入@O的时候会出现代码补全列表,这个时候我想查看列表中项目的定义能够使用快捷键 opt ...
- 用react native 做的一个推酷client
用react native 做的一个推酷client 仅供大家參考.仅仅为抛砖引玉.希望大家能以此来了解react.并编写出很多其它的优质的开源库,为程序猿做出贡献. 用的的组件: Navigator ...
- python 写了一个批量拉取文件进excel文档
路径如: C:\\Users\\huaqi\\Desktop\\信息收集 “信息收集”目录下有以下子目录:[技术,客服,运营,行政] “技术”目录下有以下子文件:[小白.txt,小红.txt,小黑.t ...
- 《读书报告 – Elasticsearch入门 》----Part II 深入搜索(1)
Part II 深入搜索 搜索不仅仅是全文本搜索:数据的很大部分是结构化的值例如日期.数字.这部分开始解释怎样以一种高效地方式结合结构化搜索和全文本搜索. 第十二章 结构化搜索 结构化搜索_ 是指查询 ...
- Linux 桌面的 Dock 类程序
1.Cairo-Dock是一个Dock类软件,它支持OpenGL.提供动画及视觉效果的插件.新的Applet.重写配置面板.新增主题等功能. 2.Docky是从GNOME Do项目剥离出来的一个Doc ...
- 进阶《Python高性能编程》中文PDF+英文PDF+源代码
入门使用高性能 Python,建议参考<Python高性能编程>,例子给的很多,讲到高性能就会提到性能监控,里面有cpu mem 方法的度量,网络讲了一点异步,net profiler 没 ...
- Android中Service的一个Demo例子
Android中Service的一个Demo例子 Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding. 本文,主要贴代码,不对Servic ...
- highcharts中的x轴如何显示时分秒时间格式
上一篇文章写道:三分钟上手Highcharts简易甘特图:https://www.jianshu.com/p/d669d451711b,在官方文档里面,x轴默认为年月日. 在项目需求中,x轴要表示24 ...
- leetcode: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...