题目:

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的更多相关文章

  1. Pairs Forming LCM(素因子分解)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B    全题在文末. 题意:在a,b中(a,b<=n) ...

  2. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  3. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  4. Pairs Forming LCM (LCM+ 唯一分解定理)题解

    Pairs Forming LCM Find the result of the following code: ; i <= n; i++ )        for( int j = i; j ...

  5. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  6. 1236 - Pairs Forming LCM

    1236 - Pairs Forming LCM   Find the result of the following code: long long pairsFormLCM( int n ) {  ...

  7. Pairs Forming LCM LightOJ - 1236 素因子分解

    Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;    fo ...

  8. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  9. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

随机推荐

  1. FZOJ 2176 easy problem ( 树链剖分 )

    pid=2176" target="_blank">题目链接~~> 做题感悟:感觉做多了树链剖分的题目,有很多是树链剖分 + 想法.. 解题思路: 这题非常明 ...

  2. 在kettle中实现数据验证和检查

    在kettle中实现数据验证和检查 在ETL项目,输入数据通常不能保证一致性.在kettle中有一些步骤能够实现数据验证或检查.验证步骤能够在一些计算的基础上验证行货字段:过滤步骤实现数据过滤:jav ...

  3. Mysql Workbench初体验

    可以画图,建立表关系. 分类整理数据表. 可以直接导出sql语句. 可以导出png图片. 可以连接mysql数据库. 基本满足了各项需求. 这次初体验只是基本的功能,这个软件对于mysql还是很牛的.

  4. 深度学习 —— 使用 gensim 实现 word2vec

    在自然语言处理领域中,将单词(words)或词语(phases)映射到向量空间(vector space)中可以很容易就得到单词之间的相似度,因为向量空间中两个向量的相似度很容易求得,比如余弦相似度. ...

  5. 129.C++面试一百题(1-51)

  6. TYVJ 1935 拆点网络流

    思路: 就是一个多重匹配 把每个防御塔拆成 拆成第j次 发射的导弹 跑个网络流 //By SiriusRen #include <cmath> #include <queue> ...

  7. Postfix邮件系统安装配置视频

    Postfix邮件系统安装配置视频(文字资料详见linux企业应用案例精解),全部视频分为四个部分,详情如下: http://115.com/file/be9j4dsj#postfix-1.rar h ...

  8. cf 865 B. Ordering Pizza

    B. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...

  9. Debian9 ifconfig命令找不到解决办法

    Debian9 ifconfig命令找不到解决办法 ifconfig.route.arp和netstat等命令行工具(它们统称为net-tools),管理和排查各种网络配置.这类工具原先起源于BSD ...

  10. noip 2018 day1 T1 铺设道路 贪心

    Code: #include<cstdio> using namespace std; int main() { int last=0,ans=0; int n;scanf("% ...