题解 UVA11889 Benefit

B e n e f i t \color{blue}{Benefit} Benefit

Time Limit: 5SMemory Limit: 128MB
Total Submissions: 128Accepted: 95

D e s c r i p t i o n \color{blue}{Description} 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.

I n p u t \color{blue}{Input} Input
The first line begin with an integer T ( T ≤ 100000 ) T (T ≤ 100000) T(T100000), the number of tests. Each test that comes in a separate line contains two integers A A A and C C C ( 1 ≤ A , C ≤ 107 ) (1 ≤ A, C ≤ 107) (1A,C107).

O u t p u t \color{blue}{Output} Output
Print the lowest integer B B B such that L C M ( A , B ) = C LCM(A, B) = C LCM(A,B)=C in a single line. If no such integer exists, print ‘ N O S O L U T I O N NO SOLUTION NOSOLUTION’ instead. ( Q u o t e s f o r c l a r i t y ) (Quotes for clarity) (Quotesforclarity)

S a m p l e   I n p u t \color{blue}{Sample\ Input} Sample Input
3
2 6
32 1760
7 16

S a m p l e   O u t p u t \color{blue}{Sample\ Output} Sample Output
3
55
NO SOLUTION


题目大意为:输入两个整数 A A A C C C,求最小的整数 B B B使得   l c m ( A , B ) = C \ lcm(A,B)=C  lcm(A,B)=C。如果无解,输出“ N O S O L U T I O N NO SOLUTION NOSOLUTION”。

l c m ( a , b ) = a b g c d ( a , b ) = c lcm(a,b)=\frac{ab}{gcd(a,b)}=c lcm(a,b)=gcd(a,b)ab=c

移项得 b g c d ( a , b ) = c a \frac{b}{gcd(a,b)}=\frac{c}{a} gcd(a,b)b=ac

因为上式左边一定为整数,所以

当右边不为整数时,即 a a a不整除 c c c时,一定无解

当右边为整数时,即 a ∣ c a|c ac时,

c a = d \frac{c}{a}=d ac=d,函数 f ( a , d ) = “ 令 x g c d ( a , x ) = d 成 立 的 x 的 最 小 整 数 解 ” f(a,d)=“令\frac{x}{gcd(a,x)}=d成立的x的最小整数解” f(a,d)=gcd(a,x)x=dx

g c d ( a , d ) = 1 gcd(a,d)=1 gcd(a,d)=1时,显然 x = d x=d x=d

g c d ( a , d ) > 1 gcd(a,d) > 1 gcd(a,d)>1时,

不妨设 g c d ( a , x ) = k     ( ∗ ) gcd(a,x)=k \ \ ^{(*)} gcd(a,x)=k  ()

( ∗ ) ^{(*)} ()式代入 f ( a , d ) f(a,d) f(a,d),得 x = d ⋅ k x=d⋅k x=dk

将上式代入 ( ∗ ) ^{(*)} ()式,得 g c d ( a , d ⋅ k ) = k gcd(a,d⋅k)=k gcd(a,dk)=k

g = g c d ( a , d ) g=gcd(a,d) g=gcd(a,d),两边同时约去 g g g,可得

g c d ( a g , d g ⋅ k ) = k g gcd(\frac{a}{g},\frac{d}{g}⋅k)=\frac{k}{g} gcd(ga,gdk)=gk

因为 a g \frac{a}{g} ga d g \frac{d}{g} gd已经互质,可以将 d g \frac{d}{g} gd从上式中剔除,得到

g c d ( a g , k ) = k g gcd(\frac{a}{g},k)=\frac{k}{g} gcd(ga,k)=gk

移项得 k g c d ( a g , k ) = g \frac{k}{gcd(\frac{a}{g},k)}=g gcd(ga,k)k=g

所以 f ( a , d ) = d ⋅ k = d ⋅ f ( a g , g ) f(a,d)=d⋅k=d⋅f(\frac{a}{g},g) f(a,d)=dk=df(ga,g)

递归求解即可

代码实现如下:

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
	int X=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+(ch^'0'); ch=getchar();}
	return X*f;
}

int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}

int f(int a,int d)
{
	int g=gcd(a,d);
	if(g==1) return d;
	return d*f(a/g,g);
}

int main()
{
	freopen("input.txt","r",stdin);
	int n=read();
	for(int i=1;i<=n;++i)
	{
		int a=read(),c=read();
		if(c%a==0) printf("%d\n",f(a,c/a));
		else printf("NO SOLUTION\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值