UVa 10892 (GCD) LCM Cardinality

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

最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
 7
 8 int lcm(int a, int b) { return a / gcd(a, b) * b; }
 9
10 int main()
11 {
12     //freopen("in.txt", "r", stdin);
13
14     int n;
15     while(scanf("%d", &n) == 1 && n)
16     {
17         vector<int> a;
18         for(int i = 1; i * i <= n; i++) if(n % i == 0)
19         {
20             if(i * i == n) a.push_back(i);
21             else { a.push_back(i); a.push_back(n / i); }
22         }
23         sort(a.begin(), a.end());
24         int num = a.size(), ans = 0;
25         for(int i = 0; i < num; i++)
26             for(int j = i; j < num; j++)
27                 if(lcm(a[i], a[j]) == n)
28                     ans++;
29         printf("%d %d\n", n, ans);
30     }
31
32     return 0;
33 }

代码君

时间: 2024-10-10 18:28:13

UVa 10892 (GCD) LCM Cardinality的相关文章

Uva 11388 GCD LCM ( 数论 )

Uva  11388 GCD LCM( 数论 ) 题意: 求是否存在a,b 使得lcm(a,b) = L, gcd(a,b) = G,不存在输出-1,存在输出a,b,且a尽可能小 分析: 强行暴力是不可能的数据很大,要用llu,这里有两种思路 思路一: 由题意可知 a*b = G*L 保证a = G的倍数的情况下,枚举a再判断G*L能否整除a,最后判断b是否为a的倍数.a从G开始扫到sqrt(G*L) //输入两个整数G,L //找出a,b 使得 gcd(a,b) = G lcm(a,b) =

UVA - 11388 GCD LCM

II U C   ONLINE   C ON TEST   2 008 Problem D: GCD LCM Input: standard input Output: standard output The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the

uva 11317 - GCD+LCM(欧拉函数+log)

题目链接:uva 11317 - GCD+LCM 题目大意:给定n,求出1~n里面两两的最大公约的积GCD和最小公倍数的积LCM,在10100进制下的位数. 解题思路:在n的情况下,对于最大公约数为i的情况又phi[n/i]次.求LCM就用两两乘积除以GCD即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; ty

uva 11388 GCD LCM题解

题意:输入两个数G,L,找出两个数a,b,使得gcd(a,b)=G,lcm(a,b)=L.有多解输出a最小的那个.无解输出-1. 嗯,这是一道值得思考5s的题目. L%G==0即有解,否则无解. 有解的话我们让a=G,b=L.这样a一定是最小,且合法. 1 #include<cstdio> 2 int T,G,L; 3 int main() 4 { 5 scanf("%d",&T); 6 while(T--) 7 { 8 scanf("%d%d"

UVA - 10892 LCM Cardinality (枚举因子)

A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. Forexample 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs withLCM is equal to

UVA 10892 LCM Cardinality (分解因数+暴力)

LCM Cardinality A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible pairs. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, the number of different integer pairs wit

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. For example 12 is the LCM of (1, 12), (2, 12), (3,4) etc. For a given positive integer N, thenumber of di?erent integer pairs with LCM is equal to N

LCM Cardinality 暴力

LCM CardinalityTime Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem FLCM CardinalityInput: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of numbers has a unique LCM but a singl

POJ 2429 GCD &amp; LCM Inverse

设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd=a1*a2 综上所诉,a1=b2,a2=b1. 也就是说,ans1=gcd*k1,ans2=gcd*k2 要求k1,k2尽量接近,并且k1,k2互质,并且,k2*k2=lcm/gcd 需要用到Pollard_rho分解质因数,然后暴力搜索寻找k1,k2.用了kuangbin大神的Pollard_rh