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

UVA - 11889 Benefit

时间:2014-08-18 12:37:24      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

Description

bubuko.com,布布扣


  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 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 ( Tbubuko.com,布布扣100000), the number of tests. Each test that comes in a separate line contains two integers A and C ( 1bubuko.com,布布扣A, Cbubuko.com,布布扣107).

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

题意:已知lcm(A, B) = C ,现在知道a和c,求最小的b。
思路:设A = a*b*c*d, B = a*b*c*e,那么我们可以清楚C = a*b*c*d*e,将A和C相除的话,我们就可以得到B除了gcd以外的部分了,
举两个例子:A = a*b*c*d, B = a*b*c*a*b*c*e; A = a*a*a, B = a*a*a*a*b,可以得到如果我们得到的部分和A互质的话,那么这个数就是最小的了,如果不是的话
不断的取gcd来补B的因子,起初以为只要一次就够了,没想到第二个样例
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
 
int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}

int main() {
	int t, A, B, C;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &A, &C);
		if (C % A) {
			printf("NO SOLUTION\n");
			continue;
		}
		B = C/A;
		int g =  gcd(A, B);
		while (g != 1) {
			B *= g;
			A /= g;
			g = gcd(A, B);
		}
		printf("%d\n", B);
	}
	return 0;
}


UVA - 11889 Benefit,布布扣,bubuko.com

UVA - 11889 Benefit

标签:des   style   http   color   os   io   strong   for   

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

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