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 自定义ratingbar 图片显示不全的解决方案
在res/style中自定义评分条: <!-- 自定义评分条 --> <style name="roomRatingBar" parent="@andr ...
- Ligerui Grid组件--学生信息列表
一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...
- 如何解决Mac与iPhone之间handoff连接问题
首先账户以及设备handoff开关问题不再赘述.主要是昨天发现的一个小技巧 当确认所有设备的iCloud账号统一.蓝牙打开.处在同一WiFi下的前提下,我的iPhone和Mac仍然handoff连接有 ...
- [book]awesome-machine-learning books
https://github.com/josephmisiti/awesome-machine-learning/blob/master/books.md Machine-Learning / Dat ...
- 2879: [Noi2012]美食节 - BZOJ
Description CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他很快就尝遍了美食节所有的美食.然而,尝鲜的欲望是难以满足的.尽 ...
- uoj 67 新年的毒瘤 割点
题目链接: 题目 #67. 新年的毒瘤 问题描述 辞旧迎新之际,喜羊羊正在打理羊村的绿化带,然后他发现了一棵长着毒瘤的树. 这个长着毒瘤的树可以用 nn 个结点 mm 条无向边的无向图表示.这个图中有 ...
- Win7(包括32和64位)使用GitHub
关于安装路径:32位可选择安装目录,但64位建议使用默认安装目录,否则Git Extensions配置会出问题 安装参考网址 http://code.google.com/p/tortoisegit/ ...
- ASP.NET MVC 4 插件化架构简单实现-实例篇
先回顾一下上篇决定的做法: 1.定义程序集搜索目录(临时目录). 2.将要使用的各种程序集(插件)复制到该目录. 3.加载临时目录中的程序集. 4.定义模板引擎的搜索路径. 5.在模板引擎的查找页面方 ...
- 编写一段程序,从标准输入读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止。使用while循环一次读取一个单词,当一个单词连续出现两次是使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有人任何单词是重复出现的。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- maven3常用命令、java项目搭建、web项目搭建详细图解
http://blog.csdn.net/edward0830ly/article/details/8748986 ------------------------------maven3常用命令-- ...