我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会。

最简单的就是枚举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的更多相关文章

  1. UVA - 11388 GCD LCM

    II U C   ONLINE   C ON TEST  Problem D: GCD LCM Input: standard input Output: standard output The GC ...

  2. UVA 11388 - GCD LCM 水~

    看题传送门 题目大意: 输入两个数G,L找出两个正整数a 和b,使得二者的最大公约数为G,最小公倍数为L,如果有多解,输出a<=b且a最小的解,无解则输出-1 思路: 方法一: 显然有G< ...

  3. UVA 10892 - LCM Cardinality

    Problem F LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair ...

  4. 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 ...

  5. UVA 10892 LCM Cardinality(数论 质因数分解)

    LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of number ...

  6. LCM Cardinality 暴力

    LCM Cardinality Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

  7. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

  8. 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 ...

  9. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

随机推荐

  1. Winform 文件控件 - 转

    1. OpenFileDialog private void openFileDialogBTN_Click(object sender, System.EventArgs e) { OpenFile ...

  2. 2014 Multi-University Training Contest 7

    官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uzd4.html Magical Forest http://acm.hdu.edu.cn/sho ...

  3. matlab字符串操作总结

    matlab字符串操作总结 字符串操作总结 char(S1,S2,…)利用给定的字符串或单元数组创建字符数组double(S)将字符串转化成ASC码形式cellstr(S)利用的给定的字符数组创建字符 ...

  4. MySql修改默认端口号,修改my.ini的端口号

    MySql默认端口号为3306,如果安装多个或者冲突需要修改端口号,修改my.ini的端口号就可以了,文件一般情况下在安装目录下.下面是具体说明: 方法/步骤 先在服务里停止mysql的服务器,再找到 ...

  5. POJ 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6044   Accepted: 24 ...

  6. MVC4.0中下来列表框的,两种使用方法DropDownList

    后台控制器代码 public ActionResult Drop() { var list = new List<SchoolInfo>(); list.Add(new SchoolInf ...

  7. java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)

    1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以 ...

  8. Thread的第一天学习

    1.实现线程的方法: 1)extend Thread 2)implements Runnable 2.下面代码执行哪个run方法: new Thread( new Runnable(){ public ...

  9. FileOutputStream和FileInputStream

    package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...

  10. mysql 中 isnull 和 ifnull 判断字段是否为null

    对于统计count(type)和avg(type) 都不起作用 SQL中有ISNULL方法,介绍如下: ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_expressi ...