题目链接: 传送门

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. lecture9-提高模型泛化能力的方法

    HInton第9课,这节课没有放论文进去.....如有不对之处还望指正.话说hinton的课果然信息量够大.推荐认真看PRML<Pattern Recognition and Machine L ...

  2. C# 传值给C++

    http://www.cnblogs.com/xumingming/archive/2008/10/10/1308248.html C#(.net)中的DllImport    大家在实际工作学习C# ...

  3. 绝对干货:供个人开发者赚钱免费使用的一些好的API接口

    不久前,我写了一篇文章,名为<科普技术贴:个人开发者的那些赚钱方式>,讲了一些个人开发者接私活和自己做软件加广告的一些科普知识.可是做软件,需要服务器,需要后台,对于一些小的开发者,想赚点 ...

  4. get_post

    各种http的请求协议: http://ymiter.iteye.com/blog/1922464 HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhj ...

  5. Spearman Rank(斯皮尔曼等级)相关系数及MATLAB实现

    转自:http://blog.csdn.net/wsywl/article/details/5859751 Spearman Rank(斯皮尔曼等级)相关系数 1.简介 在统计学中,斯皮尔曼等级相关系 ...

  6. We Know What @You #Tag: Does the Dual Role Affect Hashtag Adoption-20160520

    分析类的论文 1.Information publication:www2012 author: Mei qiao zhu 2.What 微博中的hashtag既可以表示谈论的内容,又可以代表一个群体 ...

  7. 曾经post为何只能通过form来提交

    当我们web程序的前台,需要有数据向后台发送时候,我们第一时间想到的就是,给我们所需要提交的用户名,密码之类的数据封装到一个<form>表单里面去,而封装完毕之后,我们需要给form的提交 ...

  8. python 2.7 简单模拟登陆网站

    举个栗子,首先创建网络会话, 然后就可以用创建的session来访问网页了. session.get(URL) #-*- coding:utf-8 -*- import requests import ...

  9. 使用 screen 管理你的远程会话

    文章转载自:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 在此只作为笔记使用,不做他用 你是不是经常需要 SSH 或者 telent ...

  10. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...