LightOJ 1236 - Pairs Forming LCM(素因子分解)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数
容易知道 n 是a, b的所有素因子取在a, b中较大指数的积
先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi
先不考虑a, b的大小 对于每个素因子pi
1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况
2. 在a中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况
那么对于每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1个
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e7+;//这里不能用typedef 1e7
int prime[maxn/]; //这里一定要除以10 否则会超内存 一共664579个素数
int cnt=;
bool isprime[maxn];
void getprime() //素筛打表
{
for(int i=;i<=maxn;i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
for(int j=i+i;j<=maxn;j+=i)
isprime[j]=;
}
}
}
int main()
{
getprime(); //先get素数表
//cout<<cnt<<endl;
int t,cas=;
cin>>t;
while(t--)
{
ll n,sum,ans=;
cin>>n;
for(int i=;i<cnt;i++)
{
sum=;
if(prime[i]*prime[i]>n) //记得break
break;
if(n%prime[i]==)
{
while(n%prime[i]==)
{
n/=prime[i];
sum++;
}
ans=ans*(sum*+);
}
}
if(n>) ans*=;
ans=ans/+;
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}
LightOJ 1236 - Pairs Forming LCM(素因子分解)的更多相关文章
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- 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【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...
- LightOJ 1236 Pairs Forming LCM 合数分解
题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...
- LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)
题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...
- 1236 - Pairs Forming LCM
1236 - Pairs Forming LCM Find the result of the following code: long long pairsFormLCM( int n ) { ...
- Pairs Forming LCM(素因子分解)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B 全题在文末. 题意:在a,b中(a,b<=n) ...
- Light oj 1236 - Pairs Forming LCM (约数的状压思想)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...
随机推荐
- IBM:领导力素质的三环模式
文章来源:http://blog.vsharing.com/sdtcj/A1826934.html 在翰威特公司于2003年评选的美国企业“领导人才最佳雇主”名单上,IBM名列榜首.而纽约< ...
- HttpClient连接池的连接保持、超时和失效机制
HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...
- Hibernate框架之get和load方法的区别
我们在学习Hibernate框架时,经常会进行修改,删除操作,对于这些操作,我们都应该先加载对象,然后在执行或删除的操作,那么这里Hibernate提供了两种方法按照主键加载对象,也就是我要说的get ...
- CSS相邻兄弟选择器
相邻兄弟选择器定义:选择紧接在另一个元素后的元素,而且两者有着相同的父元素. 代码一:<body> <h1>This is a heading.</h1> < ...
- Redis学习笔记之ABC
Redis学习笔记之ABC Redis命令速查 官方帮助文档 中文版本1 中文版本2(反应速度比较慢) 基本操作 字符串操作 set key value get key 哈希 HMSET user:1 ...
- C++基础知识(1)----文件操作
参照 小菜鸟上校 的博客 // file operat.cpp : 定义控制台应用程序的入口点. /*上述例子的主要功能是将一个文件的内容复制到另一个文件中, 这个功能主要由一个函数copy来实现.它 ...
- 使用srvany.exe将任何程序作为Windows服务运行
使用srvany.exe将任何程序作为Windows服务运行 2011 年 3 月 7 日 !本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使 ...
- GFF3格式
GFF3是GFF注释文件的新标准.文件中每一行为基因组的一个属性,分为9列,以TAB分开. 依次是: 1. reference sequence:参照序列 指出注释的对象.如一个染色体,克隆或片段.可 ...
- HttpGet
private static void func_httpGet(String url) { HttpClient httpClient = new HttpClient(); try { GetMe ...
- WebApp的前端所遇问题
2015年10月1日至10月25日,两个本科生和一位研究生以及一位老师组成四人团队,开发某教育前端项目 所遇问题总结以及分享: 一.主要应用软件:sublime_text HBuilder 初学者可以 ...