UVA11889:Benefit(已知LCM和其中一个数,求另一个数)

本文介绍了一种解决特定数学问题的算法,该问题是关于找到能够使两个数的最小公倍数等于给定值的合适数量。通过逐步分解问题并利用最大公约数(GCD),程序能有效地帮助学生在支付时给出正确的金额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



首先对于C不能整除A的状况肯定排除

然后得到B=C/A

然后取G=GCD(A,B)

如果G==1,那么此时B就是解

否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G

那么我们就去掉这个公因子,方法是A/G,B*G

循环直到G==1为止


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

int main()
{
    int t,a,b,c,r;
    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),k= 1;
        while(g!=1)
        {
            k*=g;
            a/=g;
            g = gcd(a,b);
        }
        printf("%d\n",b*k);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值