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以 ...
随机推荐
- html 图像映射
个人先做而一个例子 <body> <img src="图像映射/enterdesk.com-D69055E2B422567CB273963EA05FF7D4.jpg&quo ...
- Ubuntu 14 安装 Chrome/Chromium flash播放器
在命令终端中执行如下命令即可: sudo apt-get install pepperflashplugin-nonfree sudo update-pepperflashplugin-nonfree ...
- W3Cschool菜鸟教程离线版下载链接
请在电脑上打开以下链接进行下载w3cschool 离线版(chm):http://pan.baidu.com/s/1bniwRCV(最新,2014年10月21日更新)w3cschool 离线版(htm ...
- HNU 12885 Bad Signal(模拟)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12885&courseid=274 解题报告:一共有n个 ...
- iOS开发——UI进阶篇(十四)modal
一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...
- 全栈工程师学习Linux技术的忠告
随着科技的普及,Linux作为最受欢迎的服务端操作系统,无人不知,无人不晓.当今,不论是服务器搭建,还是客户端开发,Linux系统的基础技能对全栈来说都是必备的,而了解如下几个问题可以更好的帮助你成为 ...
- autolayout的各种坑
xocde7的autolayout 在viewDidLoad之前, 使用frame改变布局是没有用的, 简单的视图才可以使用autolayout, 稍微复杂写的都要使用代码来编写 获取当前view的宽 ...
- Android 中的消息传递,详解广播机制
--------------------------------------广播机制简介--------------------------------------------- Android中的广 ...
- ndk学习19: 使用Eclipse调试so
1. 设置调试选项 在AndroidManifest文件加入允许调试 android:debuggable="true" 此时编译项目会多出: 2. 配置调试代码 把需要调 ...
- ASIO插件 真的能提升 音质?(听音乐者必看)
最近在倒弄HIFI音乐播放器footbar2000的配置时,发现了2011年的一个神贴, 最牛逼的是,这个神贴到现在还屹立不倒,还有很多无知的人在下面膜拜, 我真的想问:你这么优秀,都能逆天反转音质, ...