L i n k Link Link
D e s c r i p t i o n Description Description (大意)
对于一个 x y \frac{x}{y} yx而言,我们可以将它变为 y x + y \frac{y}{x+y} x+yy 或 x + y y \frac{x+y}{y} yx+y,那么现在有一个最终结果 a b \frac{a}{b} ba,求从 1 1 1变为 a b \frac{a}{b} ba需要多少次变化
D e s c r i p t i o n Description Description (原题)
I n p u t Input Input
O u t p u t Output Output
S a m p l e Sample Sample I n p u t Input Input
S a m p l e Sample Sample O u t p u t Output Output
H i n t Hint Hint
T r a i n Train Train o f of of T h o u g h t Thought Thought
首先先将 a b \frac{a}{b} ba化为最简分数,然后一步步逆推就好了
C o d e Code Code
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
int T;
long long a, b, ans;
ll gcd(ll x, ll y)
{
if (!y) return x;
return gcd (y, x % y);
}//求最大公因数
int main()
{
// freopen("capacitor.in", "r", stdin);
// freopen("capacitor.out", "w", stdout);
scanf("%d", &T);
for (int i = 1; i <= T; ++i)
{
ans = 0;
scanf("%lld%lld", &a, &b);
ll t = gcd (a, b);
a /= t;
b /= t;
if (a < b) swap(a, b);
while (b != 1)
{
a -= b; ans++;
if (a < b) swap(a, b);
}//逆推
printf("%lld\n", a + ans);
}
}