题意:
给你一个只有a与a+b量程的烧杯,有两种操作:
1:将烧杯装满水或魔法药水
2:倒掉一些混合药水,留下a。
问你是否能配出x:y的水与药水比例。
分析:
先将x:y,a:b去除公因数。
将a:b变成1:g,发现答案必定是(1+g)的次方,并且独立起来每个(1+g)进制下都有且只能有一个g。
比如1:4,x与y某个数5进制下都为4,1:0.5,x与y某个数1.5进制下都为0.5。
如果b>=a,则(a+b)进制满足第i项大于前面所有项之和,检查x,y不断减去大的一个即可。
如果b<a,则高进制必定带有更多小数点,检查x,y哪个%s^i!=0就代表有这个数,减去。
最后检查x与y是否全为零。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <iomanip>//io控制头文件 cout<<fixed<<setprecision(8);
//#include <unordered_map>
#include <bitset>
//#pragma GCC optimize(2)
#define ms(x,n) memset(x,n,sizeof x)
#define endl '\n'
#define pii pair<int,int>
#define M(a,b) make_pair(a,b)
#define fi first
#define se second
#define pb push_back
#define __builtin_popcount popcnt1//getcnt
#define db1(x) cerr<<#x<<"="<<(x)<<" "
#define db2(x) cerr<<#x<<"="<<(x)<<"\n"
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll _gcd(ll a, ll b) { return b > 0 ? _gcd(b, a % b) : a; }
int popcnt1(ll x) { int ans = 0;for (int j = 0;j <= 60;j++)if (x & (1ll << j)) ans++;return ans; }
template <typename T> inline void read(T& t) { int f = 0, c = getchar(); t = 0;while (!isdigit(c)) f |= c == '-', c = getchar();while (isdigit(c)) t = t * 10 + c - 48, c = getchar();if (f) t = -t; }
template <typename T> void print(T x) { if (x < 0) x = -x, putchar('-');if (x > 9) print(x / 10);putchar(x % 10 + 48); }
const double pi = acos(-1.0);
const double eps = 1e-7;//有时候精度可能不够
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int N = 100+10;
int n, m,s, t, k;
int a,b,x,y;
int facm[N], facs[N],cntm,cnts;
void init() {
facm[0] = 1, cntm = 0;
for (int i = 1;i < N;i++) {
facm[i] = facm[i - 1] * m;
if (facm[i] < 0 || facm[i]>2e18) {
break;
}
cntm++;
}
facs[0] = 1, cnts = 0;
for (int i = 1;i < N;i++) {
facs[i] = facs[i - 1] * s;
if (facs[i] < 0 || facs[i]>2e18) {
break;
}
cnts++;
}
}
bool check1(int x, int y) {
int now, cnt;
for (int i = 1;i <= n;i++) {
cnt = 0;
if (x % facs[i] != 0) cnt++, now = 0;
if (y % facs[i] != 0) cnt++, now = 1;
if (cnt == 2) {
return 0;
}
if (!now) {
x -= facs[i - 1] * facm[(n - 1) - (i - 1)];
if (x < 0) return 0;
}
else {
y-= facs[i - 1] * facm[(n - 1) - (i - 1)];
if (y < 0) return 0;
}
}
return (x == 0) && (y == 0);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> t;
while (t--) {
cin >> x >> y >> a >> b;
int ab = _gcd(a, b), xy = _gcd(x, y);
a /= ab, b /= ab, x /= xy, y /= xy;
m = a + b, s = a;
init();
bool fg = 0;
int ans = 0;
for (int i = 1;i <= cntm;i++) {
if (x + y == facm[i]) {
n=ans = i;break;
}
}
if (!ans) {
cout << -1 << endl;continue;
}
if (a > b) {
if (x >= facs[n]) {
int resx = x - facs[n], resy = y;
if (resx % (m - s) == 0 && resy % (m - s) == 0) {
resx /= (m - s), resy /= (m - s);
if (check1(resx, resy)) fg = 1;
}
}
if (!fg && y >= facs[n]) {
int resx = x, resy = y-facs[n];
if (resx % (m - s) == 0 && resy % (m - s) == 0) {
resx /= (m - s), resy /= (m - s);
if (check1(resx, resy)) fg = 1;
}
}
}
else {
bool fg2 = 1;
for (int i = 0;i <= n;i++) {
if (i == n) {
if (max(x, y) >= facs[i]) {
if (x >= facs[i]) x -= facs[i];
else y -= facs[i];
}
else {
fg2 = 0;
}
}
if (max(x,y) >= (m - s) * facs[i] * facm[n - 1 - i]) {
if (x >= (m - s) * facs[i] * facm[n - 1 - i]) {
x -= (m - s) * facs[i] * facm[n - 1 - i];
}
else {
y-= (m - s) * facs[i] * facm[n - 1 - i];
}
}
else {
fg2 = 0;
}
}
if (fg2&&x == 0 && y == 0) {
fg = 1;
}
}
if (fg) {
cout << ans+1 << endl;
}
else {
cout << -1 << endl;
}
}
return 0;
}
/*
100
1000000000000000000 1 1000000000000000000 1
*/