8000 Create 6064.cpp · twinkite/basic-algo-lecture@8cea89c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8cea89c

Browse files
Create 6064.cpp
1 parent dde55cd commit 8cea89c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

0x12/6064.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int gcd(int a, int b){
5+
if(a == 0) return b;
6+
return gcd(b%a, a);
7+
}
8+
9+
int lcm(int a, int b){
10+
return a / gcd(a, b) * b;
11+
}
12+
13+
int solve(int m, int n, int x, int y){
14+
if(x == m) x = 0;
15+
if(y == n) y = 0;
16+
int l = lcm(m, n);
17+
for(int i = x; i <= l; i += m){
18+
if(i == 0) continue;
19+
if(i % n == y)
20+
return i;
21+
}
22+
return -1;
23+
}
24+
25+
int main(void){
26+
ios::sync_with_stdio(0);
27+
cin.tie(0);
28+
int n;
29+
cin >> n;
30+
while(n--){
31+
int m,n,x,y;
32+
cin >> m >> n >> x >> y;
33+
cout << solve(m, n, x, y) << '\n';
34+
}
35+
}

0 commit comments

Comments
 (0)
0