类似gcd,节点向根行走。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int a, b;
void work()
{
int l = 0, r = 0;
while (a != 1 && b != 1)
{
if (a > b)
{
l += a / b;
a = a % b;
}else
{
r += b / a;
b = b % a;
}
}
if (a == 1)
r += b - 1;
else
l += a - 1;
printf("%d %d\n\n", l, r);
}
int main()
{
//freopen("t.txt", "r", stdin);
int t, s = 0;
scanf("%d", &t);
while (t--)
{
s++;
printf("Scenario #%d:\n", s);
scanf("%d%d", &a, &b);
work();
}
return 0;
}