码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 10892 LCM Cardinality (枚举因子)

时间:2014-08-18 16:25:24      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   strong   for   ar   

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 (枚举因子),布布扣,bubuko.com

UVA - 10892 LCM Cardinality (枚举因子)

标签:style   blog   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/u011345136/article/details/38661207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!