2
1 1
3 2
2
6 5
199 200
1
3
6
200
其实这道题就是一道数论,把分数先化简后,然后一步步逆推到1,然后记录步数即可.
#include
#include
#include
#include
#define ll long long
using namespace std;
ll n,a,b,gys,ans;
ll gcd(ll x,ll y)//求最大公因数
{
ll xx = x,yy = y,r = xx % yy;
while(r)
{
xx = yy;
yy = r;
r = xx % yy;
}
return yy;
}
int main()
{
// freopen("capacitor.in","r",stdin);
// freopen("capacitor.out","w",stdout);
scanf("%lld",&n);
for(ll i = 1;i <= n; ++i)
{
scanf("%lld%lld",&a,&b);
ans = 0;
gys=gcd(a,b); //最大公因数
a /= gys;//化简因数
b /= gys;//同上
while(a != 0 && b != 0)
{
if(a > b)//看是否递减的
{
ans += a/b;//记录步数
a%=b;
}
else
{
swap(a,b);//不断往上
ans += a/b;//记录步数
a %= b;
}
}
printf("%lld\n",ans);
}
return 0;
}