fork download
  1. #include<bits/stdc++.h>
  2. #define ull unsigned long long
  3. #define ll long long
  4. #define all(x) x.begin(), x.end()
  5. using namespace std;
  6. const int maxn = 1e6 + 5;
  7. int SOLVE(int a, int b) {
  8. if (a == b) return 0;
  9. int res = INT_MAX;
  10. for (int i = 2; i <= sqrt(a); ++i) {
  11. if (a % i == 0) {
  12. if(b%i==0) res = min(res, ((b / i) * i) - b);
  13. res = min(res, ((b / i + 1) * i) - b);
  14. if (a / i != i) {
  15. if(b%(a/i)==0) res = min(res, ((b / (a / i)) * (a / i)) - b);
  16. res = min(res, ((b / (a / i) + 1) * (a / i)) - b);
  17. }
  18. }
  19. }
  20. if (res == INT_MAX) {
  21. return abs(a - b);
  22. }
  23. return res;
  24. }
  25. int main() {
  26. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  27. int n, a, b;
  28. cin >> n;
  29. while (n--) {
  30. cin >> a >> b;
  31. cout << SOLVE(a, b) << endl;
  32. }
  33. }
  34.  
  35.  
Success #stdin #stdout 0.01s 5280KB
stdin
3 
15 7
23 11	
35 42	
stdout
2
12
0