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
N can be called the LCM cardinality of that number
N. In this problem your job is to find outthe LCM cardinality ofa 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 linecontaining a single zero. This line should not be processed.

Output

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

SampleInput                             Outputfor Sample Input

2
12
24
101101291
0

2 2

12 8

24 11

101101291 5


Problemsetter: Shahriar Manzoor

Special Thanks: Derek Kisman

题意:给你n,统计有多少个整数对a<=b,满足lcm(a, b) = n,

思路:枚举所有的因子计算

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);;
}

ll lcm(ll a, ll b) {
	return a/gcd(a, b)*b;
}

ll n;

int main() {
	while (scanf("%lld", &n) != EOF && n) {
		vector<ll> ve;
		for (ll i = 1; i * i <= n; i++) {
			if (n % i == 0) {
				ve.push_back(i);
				if (i * i != n)
					ve.push_back(n/i);
			}
		}
		ll size = ve.size();
		ll ans = 0;
		for (ll i = 0; i < size; i++)
			for (ll j = i; j < size; j++)
				if (lcm(ve[i], ve[j]) == n)
					ans++;
		printf("%lld %lld\n", n, ans);
	}
	return 0;
}

UVA - 10892 LCM Cardinality (枚举因子)

时间: 2024-10-06 13:37:58

UVA - 10892 LCM Cardinality (枚举因子)的相关文章

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

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 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 11889-Benefit(数学_快速枚举因子)

Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B equals to C and he will pay back a round bill. Or otherwi

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

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

uva Fire Station(FLODY+枚举)(挺不错的简单题)

消防站 题目链接:Click Here~ 题意分析: 就是给你f个消防站,n个路口.要你求出在已有消防站的基础上在n个路口的哪个路口上在建立一个消防站,使得n个路口的到离自己最近的消防站最近的距离中最大的一个值最小.即:求n个最近路口中最大的一个,使其改最大值最小.详细的要求自己看题目吧~ 算法分析: 因为,是n个路口到每个消防站的距离.所以,我们可以想到先用一次Flody算法.把每两点的最近距离给算出来.之后在枚举N个路口,进行判断比较得出答案. #include <iostream> #i

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