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以 ...
随机推荐
- 关于phpcms中mysql和mysqli的区别
用phpcms开发一个考试成绩查询的小模块,用电脑上以前下载的phpcms版本为框架开发,一切顺利.想着下载一个最新版本,以后也免了升级的麻烦.于是,下载好,然后把模块目录.model数据库连接文件. ...
- BZOJ2780——[Spoj]8093 Sevenk Love Oimaster
0.题意:给定N个原始字符串S,M次查询某个特殊的字符串S'在多少个原始串中出现过. 1.分析:这个题我们第一感觉就是可以用后缀自动机来搞,然后我们发现不是本质不同的字串..求出现过的次数,也就是说多 ...
- [codeforces 339]C. Xenia and Weights
[codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...
- Exploiting the Circulant Structure of Tracking-by-Detection with Kernels(二)
之前给导师汇报时,主要是论文涉及公式的一些推导
- php DI实现实例:
<?php //DI 主要运用IoC用于解决 依赖文件共享(无需每一个依赖都手动注册) //管理应用程序中的『全局』对象(包括实例化.处理依赖关系). //可以延时加载对象(仅用到时才创建对象) ...
- Hello,World
引用自http://www.cnblogs.com/jbelial/archive/2013/05/08/3067471.html#2676127 题目: 1 public class text { ...
- replace、replaceAll、replaceFirst的区别详解
String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...
- mysql 同步
http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html http://blog.csdn.net/mycwq/article/det ...
- apache2 + virtualenv +djangocms
命令记录: cd /var/www makedir django cd django/ virtualenv env --no-site-packages source /var/www/django ...
- idea java 正则表达式匹配替换
原文匹配中文 excelMap.get\((\"[\u4E00-\u9F15]+\")\) 目标 excelMap.get\($1.hashCode\(\)\)