UVa 10892 (GCD) LCM Cardinality
我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会。
最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; int gcd(int a, int b) { return b == ? a : gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } int main()
{
//freopen("in.txt", "r", stdin); int n;
while(scanf("%d", &n) == && n)
{
vector<int> a;
for(int i = ; i * i <= n; i++) if(n % i == )
{
if(i * i == n) a.push_back(i);
else { a.push_back(i); a.push_back(n / i); }
}
sort(a.begin(), a.end());
int num = a.size(), ans = ;
for(int i = ; i < num; i++)
for(int j = i; j < num; j++)
if(lcm(a[i], a[j]) == n)
ans++;
printf("%d %d\n", n, ans);
} return ;
}
代码君
后来在网上找到一种这样的解法,赞叹其精妙,效率要高很多。

#include <cstdio>
#include <cmath> const int maxn = sqrt( + 0.5);
bool vis[maxn + ];
int prime[], pcnt = , e[], cnt;
int suffix[];
int f(int n)
{
if(n == cnt) return ;
return e[n] * suffix[n+] + f(n + );
} int main()
{
int m = sqrt(maxn +0.5);
for(int i = ; i <= m; i++) if(!vis[i])
for(int j = i*i; j <= maxn; j += i) vis[j] = true;
for(int i = ; i <= maxn; i++) if(!vis[i]) prime[pcnt++] = i;
//printf("%d\n", pcnt); int n;
while(scanf("%d", &n) == && n)
{
printf("%d ", n);
cnt = ;
for(int i = ; i < pcnt && n > ; i++) if(n % prime[i] == )
{
e[cnt] = ;
while(n % prime[i] == ) { e[cnt]++; n /= prime[i]; }
cnt++;
}
if(n > ) e[cnt++] = ; suffix[cnt] = ;
for(int i = cnt - ; i >= ; i--)
suffix[i] = suffix[i + ] * (e[i]*+);//后缀的乘积
printf("%d\n", f());
} return ;
}
代码君
UVa 10892 (GCD) LCM Cardinality的更多相关文章
- UVA - 11388 GCD LCM
		
II U C ONLINE C ON TEST Problem D: GCD LCM Input: standard input Output: standard output The GC ...
 - UVA 11388 - GCD LCM 水~
		
看题传送门 题目大意: 输入两个数G,L找出两个正整数a 和b,使得二者的最大公约数为G,最小公倍数为L,如果有多解,输出a<=b且a最小的解,无解则输出-1 思路: 方法一: 显然有G< ...
 - UVA  10892 - LCM Cardinality
		
Problem F LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair ...
 - UVA 10892  LCM Cardinality  数学
		
A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...
 - UVA 10892 LCM Cardinality(数论  质因数分解)
		
LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of number ...
 - LCM Cardinality     暴力
		
LCM Cardinality Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
 - Mathematics:GCD & LCM Inverse(POJ 2429)
		
根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...
 - POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
		
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
 - [POJ 2429] GCD & LCM Inverse
		
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
 
随机推荐
- IR的评价指标—MAP,NDCG,MRR
			
http://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是每篇相关文 ...
 - .NET操作JSON
			
http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html JSON文件读入到内存中就是字符串,.NET操作JSON就是生成与 ...
 - C# type - IsPrimitive
			
Type t = typeof(string); if (t.IsPrimitive)//not { Console.WriteLine("string is a Primitive&quo ...
 - C# 客户端判断服务器连接已断开
			
问题描述: 在C# Socket编程中,服务器端已经断开连接(发送数据方),客户端接收服务器端发送数据,在客户端使用client.Recieve()中,服务器端断开连接,客户端任然显示已 ...
 - boost序列化
			
#include <iostream> #include <boost/serialization/serialization.hpp> #include <boost/ ...
 - js 判断是否为chrome浏览器
			
var isChrome =navigator.userAgent.indexOf("Chrome") !== -1 用 navigator.appVersion 不好使,因为al ...
 - Codeforces Round #363 (Div. 2)->B. One Bomb
			
B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
 - log4j使用感受
			
1.为什么使用日志? 日志可以记录项目中的重要信息,关键输出信息,异常信息,为项目上线后期维护提供方便,在项目开发中尽量养成习惯写日志,而不是System.out.println()打印,不过在jun ...
 - 【C++基础】 类中static  private  public protected
			
静态成员在一个类的所有实例间共享数据 “类属性”,是描述类的所有对象共同特征的一个数据项,对所有对象,它的值相同,static定义,为整个类所共有.相对于“实例属性” 如果static成员是私有类型, ...
 - Caching Tutorial
			
for Web Authors and Webmasters This is an informational document. Although technical in nature, it a ...