题目链接: 传送门

Benefit

Time Limit: 5000MS     Memory Limit: 32768 KB

Description

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

解题思路:

题目大意:给出A、C,为你A与哪个数的最小公倍数是C。
其实就是欧几里得的应用,若C不是A的整数倍,直接跳出,若是,看一下A,C/A的最大公因数是不是1,如果不是继续跑欧几里得。

#include<iostream>
#include<cstdio>
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 == 0)
        {
            B = C/A;
            int tmp = gcd(A,B);
            if (tmp == 1)
            {
                printf("%d\n",C/A);
            }
            else
            {
                int res = 1;
                while (tmp != 1)
                {
                    res *= tmp;
                    A = A / tmp;  //如果改用B = B/tmp去做的话。。一直超时,不知道是数据的原因还是什么。没搞懂
                    tmp = gcd(A,B);
                }
                printf("%d\n",res*B);
            }
        }
        else
        {
            printf("NO SOLUTION\n");
        }
    }
    return 0;
}

UVa 11889 Benefit(数论)的更多相关文章

  1. UVA 11889 - Benefit 可直接枚举

    看题传送门 题目大意: 输入两个整数A和C,求最小的整数B,使得lcm(A,B)=C.如果无解,输出NO SOLUTION 思路: A*B=C*gcd(A,B) 所以 B / gcd(A,B) = C ...

  2. UVA 11889 Benefit

    题意: lcm(a, b) = c; c是a,b的最小共倍数, 现在给出a, c, 要你求出最小的b. 解题思路:         1. 如果c%a != 0 表示无解. 设b = c/a; 当gcd ...

  3. Uva 11889 Benefit (lcm与gcd)

    题意:给你两个数,a,c,求出 lcm(a,b)==c 时的 b 的最小值 思路:我们知道一个性质 gcd(a,b)*lcm(a,b) = a*b 由此我们可以得到 b = gcd(a,b)*lcm( ...

  4. 数论 UVA 11889

    有关数论的题目,题目大意是给你两个数a和c,c为a和另一个数b的最小公倍数,要求你求出b的最小值.由最大公约数gcd(a,b)和最小公倍数lcm(a,b)之间的关系可知,lcm(a,b)*gcd(a, ...

  5. UVa 11889 (GCD) Benefit

    好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...

  6. Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)

    首先对于C不能整除A的状况肯定排除 然后得到B=C/A 然后取G=GCD(A,B) 如果G==1,那么此时B就是解 否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G 那么我 ...

  7. uva 10127 - Ones(数论)

    题目链接:uva 10127 - Ones 题目大意:给出n,问说者少要多少为1才干够整除n. 解题思路:等于是高精度取模,直到余数为0为止. #include <cstdio> #inc ...

  8. uva 1434 - YAPTCHA(数论)

    题目链接:uva 1434 - YAPTCHA 题目大意:给定n和k,求题目中给定的式子S(n). 解题思路:威尔逊定理,x为素数时有,((x−1)!+1)%x==0,所以对于本题.假设3*k+7为素 ...

  9. UVA 11645 - Bits(数论+计数问题)

    题目链接:11645 - Bits 题意:给定一个数字n.要求0-n的二进制形式下,连续11的个数. 思路:和 UVA 11038 这题相似,枚举中间,然后处理两边的情况. 只是本题最大的答案会超过l ...

随机推荐

  1. MFC添加背景图片

    1.在资源里导入一个bmp图片假设名称为IDB_BITMAP1 实现OnPaint方法 CPaintDC dc(this); CRect rect; GetClientRect(&rect); ...

  2. Chrome 监听 console 打开

    这个算是 Chrome only 其他的我没测试,也不想测试.因为我的控制台脚本仅仅在 Chrome 下加载. 如果你需要全平台,那么这肯定不是你需要的结果. 需求 其实我很早就想折腾这个了,但是,, ...

  3. js 0.1+0.2!=0.3

    准确的说就是js小数采用ieee的64位的双精度,1位表示正负,11位指数,52位小数,所以对于0.1js是无法精确表示的的,所以会多点, http://www.jb51.net/article/77 ...

  4. [bzoj2286][Sdoi2011]消耗战(虚树上的DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2286 分析:对于普通的树形dp:f[x]=min(∑f[son],m[x]),其中f[ ...

  5. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  6. scrollLeft的相关问题(js横向无缝滚动)

    <div id="demo"> <div id="innerdemo"> <div id="demo1"> ...

  7. 《HTTP 权威指南》

    第一篇:  Web基础  (HTTP概述. URL.HTTP报文.连接管理) 0. scheme:方案!     协议类型 1.HTTP:超文本传输(状态转移)协议:通信协议方案.     web浏览 ...

  8. springMvc全局异常处理

    本文中只测试了:实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器 对已有代码没有入侵性等优点,同时,在异常处理时能获取导致出现异常的对象,有利于提 ...

  9. Spring + Spring MVC+Hibernate框架整合详细配置

    来源于:http://www.jianshu.com/p/8e2f92d0838c 具体配置参数: Spring: spring-framework-4.2.2Hibernate: hibernate ...

  10. eclipse-debug时直接进入/不进入/提示进入调试页面修改

    eclipse使用debug调试程序时 默认设置每次程序走到断点位置时提示是否进入调试页面(如图) 而个人习惯有些系统直接进入调试页面.也有些人系统不进入调试页面调试 在这里勾选Remember my ...