【题解】正确率
glass_goldfish · · 个人记录
原题链接
思路
无解:如果已经有通过,但是要求通过数量为
其他情况都是有解情况;设置变量
代码
#include<bits/stdc++.h>
#define int long long
#define INF 1145141919810
using namespace std;
int t,x,y,p,q;
signed main(){
cin>>t;
while(t--){
cin>>x>>y>>p>>q;
if(x!=0&&p==0||x!=y&&p==q)
cout<<"-1\n";//无解
else{
for(int i=1;;i++){
int pi=p*i,qi=q*i;
if(qi-y>=pi-x&&pi-x>=0){//有解
cout<<qi-y<<"\n";//最终的答案
break;//退出循环
}
}
}
}
return 0;
}