zoukankan      html  css  js  c++  java
  • UVA 11889 Benefit

                        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 }
  • 相关阅读:
    《20171122-构建之法:现代软件工程-阅读笔记》
    课后作业-阅读任务-阅读提问-5
    结队-贪吃蛇-项目进度
    《团队-爬取豆瓣Top250电影-团队-阶段互评》
    zip相关知识梳理(一)
    C++入职学习篇--代码规范(持续更新)
    C++入职学习篇--新员工入职(持续更新)
    Node.js学习之TCP/IP数据通讯
    Node.js之操作文件系统(二)
    Node.js之操作文件系统(一)
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4676506.html
Copyright © 2011-2022 走看看