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, ...
随机推荐
- 【Oracle错误集锦】:ORA-00119 & ORA-00132
有时候老天就是爱和你开玩笑,昨天好不easy配置好Oracle.可以用PL/SQL正常登录使用,今天突然就不行了.而且错误十分诡异,没有提示什么错误代码.输入usernamepassword,点击登录 ...
- Cordic 算法入门
三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...
- vim 基础学习之文件跳转
1. ''-当前文件上次跳转之前的位置2. '.-当前文件上次修改的位置,只要是发生了可能导致变化的命令操作就会被标记,哪怕实际结果没有变化3. '^-当前文件上次插入的位置,只要是发生了插入操作命令 ...
- Sam做题记录
Sam做题记录 Hihocoder 后缀自动机二·重复旋律5 求一个串中本质不同的子串数 显然,答案是 \(\sum len[i]-len[fa[i]]\) Hihocoder 后缀自动机三·重复旋律 ...
- Mysql基础部分,针对以后python使用
#redis 非关系型数据库#mysql 关系型数据库 表与表之间有数据关系 Oracle Mysql SqlServer DB2#多张表组合在一起就是数据库#冗余 存储两倍数据 可以使系统速度更快 ...
- OpenCV中InputArray和OutputArray使用方法
原文链接:http://blog.csdn.net/yang_xian521/article/details/7755101 看过OpenCV源代码的朋友,肯定都知道很多函数的接口都是InputArr ...
- C/C++(C++类与对象)
构造器(constructor) 1.与类名相同,无返回,被系统生成对象时自动调用,用于初始化. 2.可以有参数,构造器的重载,有默认参数.重载和默认参数不能同时出现,但是一定要包含标配(无参数的构造 ...
- Vbox下创建Linux和Windows的共享文件夹
我的Vbox版本是4.3.6...在这里以win8和Ubuntu12.04之间共享文件举例 首先运行虚拟机,然后安装增强功能..这个增强功能很碉堡...能开启无缝模式和系统间的剪贴板共享等牛X功能 然 ...
- angularCli打包遇到的一些问题
有时在运行项目或者打包项目的时候会遇到报错信息:found version 4, expected 3, 这个大概意思是说该插件需要的依赖当前不支持,需要提高依赖的版本. 比如:@angular/co ...
- 休假结束,Linus重回内核开发岗位
在休假反省一个多月之后,Linus Torvalds 又回来了.10 月 22 日爱丁堡举行的欧洲开源峰会上,Linus Torvalds 将与内核维护者们碰头,这是他重新接管Linux内核开发的第一 ...