# 2014 ACM-ICPC - K:Last Defence

Time limit 1000 ms Memory limit 262144 kB

## Description

Given two integers A and B. Sequence S is defined as follow:
• S0 = A
• S1 = B
• Si = |Si−1 − Si−2| for i ≥ 2
Count the number of distinct numbers in S.

## Input

The first line of the input gives the number of test cases, T. T test cases follow. T is about 100000. Each test case consists of one line - two space-separated integers A, B. (0 ≤ A, B ≤ 10^18).

## Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of distinct numbers in S.

2
7 4
3 5

Case #1: 6
Case #2: 5

## Problem solving:

s2 = 7, s3 = 4, s4 = 3, s5 = 1。先看这里10 7 4 1分别是10 - 3t的到的结果，能减10/3 = 3次。这3代表10 7 4这三个数。接下来相当于变成s0 = b = 3, s1 = a%b = 1 重新循环上述操作。

#include <stdio.h>
long long ans;
void gcd(long long a, long long b)
{
if (!b)
return ;
ans += a / b;
gcd(b, a % b);
}
int main()
{
int t;
long long a, b;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
ans = 1;
scanf("%lld%lld", &a, &b);
if (!a && b || a && !b)
ans++;
gcd(a, b);
printf("Case #%d: %lld\n", i, ans);
}
return 0;
}


06-01 649

10-09 23
08-05 707
10-23 270
02-28
09-28