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: ...
随机推荐
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
- python SendMail 发送邮件
最近在学习python 时,用到了发送邮件的操作,通过整理总结如下: 1.普通文本邮件 普通文本邮件发送的实现,关键是要将MIMEText中_subtype设置为plain,首先导入smtplib和m ...
- c++ 继承多个类 及虚函数
#include <iostream>using namespace std; class BaseA {public: virtual void say() { co ...
- error C2061: syntax error : identifier '__RPC__out_xcount_part'
朋友遇到的 把dx, windows sdk ,vs2010照着成功人士的配置好 应该就可以了
- ssh 内在溢出
相信有一定java开发经验的人或多或少都会遇到OutOfMemoryError的问题,这个问题曾困扰了我很长时间,随着解决各类问题经验的积累以及对问题根源的探索,终于有了一个比较深入的认识. 在解决j ...
- 你所不知道的黑客工具之 EK 篇
EK(Exploit kits)是指一套利用恶意软件感染用户电脑发起攻击的黑客工具,时下最著名的有 Angler EK.Fiesta EK.Hanjuan EK.Nuclear EK.Neutrino ...
- OneAlert:国内首家 SaaS 模式的云告警平台
随着互联网行业的高速发展,企业 IT 应用环境日趋复杂.几分钟的故障就会严重到用户体验,那么如何有效降低IT故障带来的影响呢?权威数据表明,86%的企业认为建立有效的监控系统和告警系统.提升 IT 的 ...
- SDUT2482二叉排序树
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2482&cid=1184 题目描述 二叉排序树的定义是:或者是一棵空树,或者是具有下列性质 ...
- VBScript 函数
Date/Time 函数 Conversion 函数 Format 函数 Math 函数 Array 函数 String 函数 其他函数 Date/Time 函数 函数 描述 CDate 把一个有效的 ...
- XML中如何使用schema
Schema简介 DTD的语法相当复杂,并且它不符合XML文件的标准,自成一个体系,W3C定义的Schema用来代替DTD. chema相对于DTD的明显好处是XML Schema文档本身也是XML文 ...