B e n e f i t \color{blue}{Benefit} Benefit
Time Limit: 5S | Memory Limit: 128MB | |
---|---|---|
Total Submissions: 128 | Accepted: 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(T≤100000), 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)
(1≤A,C≤107).
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 a∣c时,
令 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=d成立的x的最小整数解”
当 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=d⋅k
将上式代入 ( ∗ ) ^{(*)} (∗)式,得 g c d ( a , d ⋅ k ) = k gcd(a,d⋅k)=k gcd(a,d⋅k)=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,gd⋅k)=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)=d⋅k=d⋅f(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;
}