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: ...
随机推荐
- android studio出现 waiting for adb
cmd进入命令行,进入adb所在的目录下: 出现的鬼异问题如下. C:\Users\xxxx>adb start-server adb server is out of date. killin ...
- System.IO.StreamWriter
string path = @"D:\a.txt"; System.IO.StreamWriter swOut = new System.IO.StreamWriter(path, ...
- .net发送邮件代码示例
下面的代理已经调试过,用的是163的SMTP using System;using System.Collections.Generic;using System.Linq;using System. ...
- CodeForces369C On Changing Tree
昨天的CF自己太挫了.一上来看到A题,就有思路,然后马上敲,但是苦于自己很久没有敲计数的题了,许多函数都稍微回忆了一阵子.A题的主要做法就是将每个数质因数分解,统计每个质因子的个数,对于每个质因子pi ...
- http://my.oschina.net/u/1185331/blog/502350
http://my.oschina.net/u/1185331/blog/502350
- [2-sat]HDOJ3622 Bomb Game
题意:给n对炸弹,每对炸弹选其中一个爆炸. 每个炸弹爆炸的半径相同 圆不能相交, 求最大半径 2-sat简介 二分半径, 枚举n*2个炸弹 若i炸弹与j炸弹的距离小于半径*2 则建边 比如 第一对炸 ...
- Ubuntu Geany中文乱码
打开Geany,编辑,首选项,文件,选中“使用固定的编码打开非Unicode文件”,缺省编码选择“简体中文GBK)”. 另外,直接把文本文件拖进浏览器也行(前提是你的浏览器使用的是中文,我用的chro ...
- Java传入参数个数不确定可用(Type ... values)
/** * 可变长的参数. * 有时候,我们传入到方法的参数的个数是不固定的,为了解决这个问题,我们一般采用下面的方法: * 1. 重载,多重载几个方法,尽可能的满足参数的个数.显然这不是什么好办法. ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
- iOS开发 -- 发送JSON数据给服务器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLW ...