思路:
把b分之a化简后逆推(在纸上画一下就好了),发现分母分子与另一半对称(可用来验证),然后用代码实现就可以了。
CodeCodeCode:
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
ll a,b,sum;
int t;
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)
{
scanf("%lld%lld", &a, &b);
sum=0;
ll ab_gcd = gcd(a, b);
a /= ab_gcd,b /= ab_gcd;//化简后的
while(a&&b)//不为0
{
if(a > b){sum+=a/b;a%=b;}
else {swap(a,b);sum+=a/b;a%=b;}
}//逆推
printf("%lld\n",sum);//输出
}
return 0;
}