RT,只A了一个点,其他的要么RE要么TLE,RE报错的信息是:Received signal 8: Floating-point exception.
求助各位大佬,谢谢:
#include<bits/stdc++.h>
using namespace std;
#define debug(a, n)\
printf(#a ":");\
for(int i = 1; i <= n; ++i) cout << a[i] << ' ';\
cout << '\n';
#define int long long
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 ^ 48); ch = getchar();}
return x * f;
}
int T, x, z;
signed main() {
// freopen("math.in", "r", stdin);
// freopen("math.out", "w", stdout);
T = read();
while(T --) {
x = read(), z = read();
if(z % x != 0) puts("-1");
else{
int p = z / x;
vector<int> ys;
for(int i = 1; i * i <= p; ++i) {
if(p % i == 0) {
ys.push_back(i);
}
}
for(int i : ys) ys.push_back(p / i);
bool flag = false;
for(int t : ys) {
if(__gcd(x, t) == p / t) {
cout << t << endl;
flag = true;
break;
}
if(__gcd(x, p / t) == t) {
cout << p / t << endl;
flag = true;
break;
}
}
if(!flag) puts("-1");
}
}
return 0;
}