UVA 10892

LCM Cardinality

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

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 with LCM is equal to N can be called the LCMcardinality of that number N. In this problem your job is
to find out the LCM cardinality of a number.

Input

The input file contains at most 101 lines of inputs. Each line contains an integer N (0<N<=2*109). Input is terminated by a line containing a single zero. This line should not be processed.

Output

For each line of input except the last one produce one line of output. This line contains two integers N and C. Here N is the input number and Cis its cardinality. These two numbers are
separated by a single space.

Sample Input                             Output for Sample Input

2                                                          
12
24
101101291
0

2 2

12 8

24 11

10110129

题意:给出a和b的最小公倍数N,找出符合条件的a、b有多少对。

分析:1. 设n = LCM(a,b) = (p1^r1) * (p2^r2) * (p3^r3) … (pm^rm)

又设a=(p1^a1) * (p2^a2) * (p3^a3) … (pm^am),

b=(p1^b1) * (p2^b2) * (p3^b3)… (pm^bm)

由LCM的定义有ri = max{ai, bi}

所以对于每个ri,ai和bi中至少有一个要取ri

2. 对于ai取ri的情况,bi可以取[0,ri-1]的任意整数,这有ri种情况;

bi取ri的情况同样是ri种 。

最后加上ai和bi都取ri的情况,共有(2*ri+1)种情况

3. 由于这么考虑把(a,b)和(b,a)算重复了,但(n,n)的情况只算了一遍,所以最后要ans= (ans+1)/2=ans/2+1(因为ans是奇数)

4. 优化:只考虑√n范围内的质数,但这样会存在漏掉一个大质数的情况(比如n=2*101) ,这个大质数的幂次只能为1(即少算了一个*(2*1+1)),所以在这种情况发生时要补上ans*=3,写成 位运算就是ans+=ans<<1。

#include <cstdio>
#include <cmath>

int n;

void get_ans() {
    int tmp = n;
    int m = (int)sqrt(n + 0.5);
    long long ans = 1;
    for(int i = 2; i <= m; i += 2) {
        if(n % i == 0) {
            int cnt = 0;
            while(n % i == 0) {
                n /= i;
                cnt++;
            }
            ans *= (cnt << 1) + 1;
        }
        if(i == 2) i--;
    }
    if(n > 1) ans += (ans<<1);
    ans = (ans >> 1) + 1;
    printf("%d %lld\n", tmp, ans);
}

int main() {
    while(~scanf("%d", &n) && n) {
        get_ans();
    }
    return 0;
}
时间: 2024-10-03 05:42:19

UVA 10892的相关文章

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 (数论/暴力)

题意:给出数n,求有多少组A,B的最小公约数为n; 思路:3000ms,直接暴力寻找,找到所有能把n整除的数 pi, 枚举所有pi 代码: #include <iostream> #include <cstdio> #include <vector> #define ll long long using namespace std; ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } int

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

UVA 10892 LCM Cardinality

题目链接:UVA-10892 题意为给定一个数n,问有多少组不同的数对<a,b>的lcm等于n.看了AC,∑ndless的题解,直接摘抄了. 代码: 1 #include"cstdio" 2 #include"iostream" 3 #include"cstring" 4 #include"algorithm" 5 #include"cstdlib" 6 #include"vector

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

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te