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, ...
随机推荐
- Fragment的实际开发中总结(二)
在实际项目的开发过程Fragment的情况越来越多.大家肯定须要遇到过Fragment被销毁重建的情况. 结合自己在项目开发的一点总结和学习开源项目的代码.继续分享自己对Fragment的一点总结. ...
- Qt编译mysql以及创建表后进行导入操作
鉴于很多同学对Qt编译myql总是不能成功.出现各种问题,今天特此写出本教程,希望可以帮到须要的同学. 首先,须要明确编译的目的和原理. 目的:Qt 5.2版本号曾经都是不带mysql驱 ...
- javascript进阶课程--第一章--函数
javascript进阶课程--第一章--函数 学习要点 了解内存管理 掌握全局函数的使用 知识点 基本类型和引用类型 基本类型值有:undefined,NUll,Boolean,Number和Str ...
- hdoj--1034--Hidden String(dfs)
Hidden String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- Array 对象常用的方法总结
shift:删除原数组的第一项,返回删除元素的值:如果数组为空则返回undefined var arr = [1, 2, 3, 4, 5]; var out = arr.shift(); consol ...
- springboot 使用mybatis 通用Mapper,pagehelper
首先需要maven导入需要的包,这里用的是sqlserver,druid,jtds连接数据库 <dependency> <groupId>com.alibaba</gro ...
- 基于SIFT的点云关键点提取
这篇博文主要介绍SIFT算法在提取点云图像关键点时的具体用法. 尺度不变特征转换(Scale-invariant feature transform,SIFT)是David Lowe在1999年发表, ...
- Flex 集合 ArrayCollection 的使用
转:http://keren.iteye.com/blog/380847 转:http://callan.iteye.com/blog/335551 集合是ActionScript 中功能强大的基于索 ...
- 【js基础】判断是否是合法邮箱地址(正则表达式的应用)
2019-01-21 09:11:21 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- tensorflow学习之路----保存和提取数据
#保存数据注意他只能保存变量,不能保存神经网络的框架.#保存数据的作用:保存权重有利于下一次的训练,或者可以用这个数据进行识别#np.arange():arange函数用于创建等差数组,使用频率非常高 ...