1187 尋找分數
思路:類歐幾里得
代碼:html
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define pdd pair<double, double> #define mem(a, b) memset(a, b, sizeof(a)) #define debug(x) cerr << #x << " = " << x << "\n"; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); //head int T; LL a, b, c, d, p, q; //a/b < p/q < c/d void solve(LL a, LL b, LL c, LL d) { if(a == 0) { p = 1; q = d/c+1; return ; } if(a >= b) { solve(a%b, b, c-(a/b)*d, d); p += (a/b)*q; return ; } if(c > d) { p = q = 1; return ; } solve(d, c, b, a); swap(p, q); } int main() { scanf("%d", &T); while(T--) { scanf("%lld %lld %lld %lld", &a, &b, &c, &d); solve(a, b, c, d); printf("%lld/%lld\n", p, q); } return 0; }