记录编号 605998 评测结果 AAAAAAAAAAAA
题目名称 4177.[USACO25 Feb Silver]Transforming Pairs 最终得分 100
用户昵称 Gravatar彭欣越 是否通过 通过
代码语言 C++ 运行时间 0.149 s
提交时间 2025-09-13 17:37:16 内存使用 3.73 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100010;
ll T,a,b,c,d,ans;
int main () {
	freopen("Transforming.in","r",stdin);
	freopen("Transforming.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> T;
	while (T--) {
		ans=0;
		cin >> a >> b >> c >> d;
		while (a<=c&&b<=d) {
			if (a==c&&b==d) break;
			//cout << c <<' '<< d <<endl;
			if (c>d) {
				if (d==0) break;
				ll t=(c-a)/d;
				if (t<=0) break;
				c-=t*d;
				ans+=t;
			}else if (c<d) {
				if (c==0) break;
				ll t=(d-b)/c;
				if (t<=0) break;
				d-=t*c;
				ans+=t;
			}else break;
		}
		if (a==c&&b==d) cout << ans <<endl;
		else cout << -1 <<endl;
	}
	return 0;
}