Benefit---UVA11889最小公倍数(两种方法)

 UVA - 11889 

题目链接:https://vjudge.net/contest/321024#problem/B

       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 otherwise take some snack instead of the remaining of his money. He believes that finding such a number is hard enough that dissuades students from paying that.

       You should write a program that help poor students giving the appropriate amount of money to Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.

Input

      The first line begin with an integer T (T ≤ 100000), the number of tests. Each test that comes in a separate line contains two integers A and C (1 ≤ A, C ≤ 1e7 ).

Output

Print the lowest integer B such that LCM(A, B) = C in a single line. If no such integer exists, print ‘NO SOLUTION’ instead. (Quotes for clarity)

Sample Input

3

2 6

32 1760

7 16

Sample Output

3

55

NO SOLUTION


题目大意:给你两个整数,A,C,求最小的整数B使得A和B的最小公倍数为C,如果无解就输出NO SOLUTION;

我开始写的比较暴力,T了一发,然后想了想,用什么素数分解啊,筛出来的素数都比直接求c的因子多了。。。我还不如直接求c的因子。。。。暴力计算c的因子,复杂度为sqrt(c)大概1e3左右,再乘上T(1e5)应该可以跑过去。

我们将c的所有素因子的个数算出来(这个并不需要素数筛)用个map保存一下:

for (int i = 2; i <= sqrt(c); i++) {
	while (c % i == 0) {
		q[i]++;
		c /= i;
	}
	if (c == 1) break;
}
if (c != 1) q[c]++;

同样的求出a的所有素因子,另外用一个map保存一下。接下来就是关键了。

假我们的c的所有素因子排列如:2 2 3 5 7 7;

a的:2 2 3 7

那么b是多少呢?很显然b的因子一定是:5 7 7

也就是说当a和c的某个因子数一样时b就不需要这个因子了,否则就需要这个因子数量达到c一样的数量

那么这段代码也还好写,至于无解的情况那就只有c/a的余数不为0了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

map<int, int>q, q1;
int qpow(int a, int b)
{
	int ans = 1;
	while (b) {
		if (b & 1) ans *= a;
		b >>= 1;
		a *= a;
	}
	return ans;
}

int main()
{
	int a, c;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &a, &c);
		if (c % a) { printf("NO SOLUTION\n"); continue; }
		if (a == 1) { printf("%d\n", c / a); continue; }
		int cc = c, num = 1;
		q.clear(); q1.clear();
		for (int i = 2; i <= sqrt(c); i++) {
			while (c % i == 0) {
				q[i]++;c /= i;			
			}
			if (c == 1) break;
		}
		if (c != 1) q[c]++;
		for (int i = 2; i <= sqrt(a); i++) {
			while (a % i == 0) {
				q1[i]++; a /= i;
			}
			if (a == 1) break;
		}
		if (a != 1) q1[a]++;
		map<int, int>::iterator it;
		for (it = q.begin(); it != q.end(); it++) {
			if (q1[it->first] == it->second) q[it->first] = 0;
		}
		ll ans1 = 1;
		for (it = q.begin(); it != q.end(); it++) {
			ans1 *= qpow(it->first, it->second);
		}
		printf("%lld\n", ans1);
	}
	return 0;
}

接下来就是比较简单的方法:根据公式gcd(a,b)*lcm(a,b)=a*b可以得到:

\frac{a\times b}{gcd(a,b)}=c从而转化到\frac{b}{gcd(a,b)}=\frac{c}{a},也就是说B一定是c/a的倍数,我们之间循环找一下就好了,从c/a开始,每次加上c/a;

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main()
{
	int a, c;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &a, &c);
		if (c % a) { printf("NO SOLUTION\n"); continue; }
		int p = c / a;
		for (int i = p; ; i += p) {
			if (i / __gcd(i, a) == p) { printf("%d\n", i); break; }
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值