Created
March 23, 2023 14:17
-
-
Save qjatn0120/26e54d8e9ccdeb241cb56cfb4962a428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef DEBUG | |
#include "debug.h" | |
#endif // DEBUG | |
#ifndef DEBUG | |
template <typename T> | |
void debug(T &x){} | |
template <typename T> | |
void debug(T &x, int i, int j){} | |
#endif // DEBUG | |
#include <bits/stdc++.h> | |
using namespace std; | |
long long int gcd(long long int a, long long int b){ | |
if(b == 0) return a; | |
return gcd(b, a % b); | |
} | |
int main(){ | |
cin.tie(nullptr), ios::sync_with_stdio(false); | |
int T; | |
cin >> T; | |
while(T--){ | |
long long int A, B, C, D; | |
cin >> A >> B >> C >> D; | |
long long int rem = A % B; | |
if(B > D) swap(B, D); | |
if(rem + 1 != B) cout << "1\n"; | |
else cout << (B * D / gcd(B, D) - rem) << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment