-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPTIT016A-BCNN.cpp
More file actions
57 lines (47 loc) · 908 Bytes
/
PTIT016A-BCNN.cpp
File metadata and controls
57 lines (47 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;
long long gcd(long long a,long long b){
long long temp;
while(b){
temp=a%b;
a=b;
b=temp;
}
return a;
}
int main(void){
long long A,B,max,max1,N,N1,k;
cin>>A>>B;
if(A<B){
k=A;
A=B;
B=k;
}
if(A==B){
cout<<1;
return 0;
}
k=A-B;
N=(B/k + 1)*k - B;
max=(A+N)/gcd(A+N,B+N)*(B+N);
for(long long x=2;x*x<=k;x++){
if(k%x==0)
{
N1=(B/x+1)*x-B;
if(N1<N)
{
max1=(A+N1)/gcd(A+N1,B+N1)*(B+N1);
if(max>=max1) {max=max1;N=N1;}
}
long long y=k/x;
N1=(B/y+1)*y-B;
if(N1<N)
{
max1=(A+N1)/gcd(A+N1,B+N1)*(B+N1);
if(max>=max1) {max=max1;N=N1;}
}
}
}
cout<<N;
return 0;
}