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

UVA 11889 Benefit

时间:2015-07-25 21:26:53      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

                    11889 Benefit

题意:

  给出T(T ≤ 100000)组数据;每组数据中给出A,C (1 ≤ A, C ≤ 10e7);已知LCM(A,B)=C,求最小的B

  如果无解的话,输出 NO SOLUTION

 

思路:

  比较难懂,可以多思考几遍。首先b=c/a;如果b不是int;直接无解;然后循环(对A和B求GCD的一值,再A/=值)直到那个值为1;

  反例:12 16 48,顺便借这个例子分析一下:b=c/a=48/12=4,12和4的LCM是12不是48因为他们的有GCD影响,然后GCD的那个循环的值为4,答案4*4


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 ≤ 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

代码如下:

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 int gcd(int m,int n)
 6 {
 7     if(m<n)
 8     {
 9         int t=m;
10         m=n;
11         n=t;
12     }
13     if(m%n==0)
14         return n;
15     else
16         return gcd(n,m%n);
17 }
18 
19 int main()
20 {
21     int t;
22     cin>>t;
23     while(t--)
24     {
25         int a,c;
26         cin>>a>>c;
27         if(c%a==0)
28         {
29             int b2=c/a;
30             int ans=1;
31             int y=0;
32             while(y!=1)
33             {
34                 y=gcd(b2,a);
35                 ans*=y;
36                 a/=y;
37             }
38             cout<<b2*ans<<endl;
39         }
40         else
41             cout<<"NO SOLUTION"<<endl;
42     }
43     return 0;
44 }

 

UVA 11889 Benefit

标签:

原文地址:http://www.cnblogs.com/moqitianliang/p/4676506.html

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