UVALive 7045 Last Defence(2014西安赛区现场赛)

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 ≤ 1018).

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.

Sample Input:
2
7 4
3 5
Sample Output:
Case #1: 6
Case #2: 5

题意:有一个序列,它的规律是Sn = |Sn-1 - Sn-2|(n >= 2),现在知道S1和S2,求出这个序列中出现不同数字的个数。

(我也不太明白为啥这样写,目测是根据观察得出的结论)

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main ()
{
    int T, k = 0;
    long long a, b, ans;

    scanf("%d", &T);

    while (T--)
    {
        scanf("%lld %lld", &a, &b);

        k++;
        ans = 0;

        if (a == 0 && b == 0) ///这里是最容易遗漏的地方
        {
            printf("Case #%d: %lld\n", k, ans+1);
            continue;
        }

        if (a == 0 || b == 0) ans = 1;

        while (a || b)
        {
            if (a < b) swap(a, b);

            if (b == 0)
            {
                ans++;
                break;
            }

            ans += a / b;
            a = a % b;
        }

        printf("Case #%d: %lld\n", k, ans);
    }

    return 0;
}
posted @ 2015-10-09 14:53  搁浅の记忆  阅读(277)  评论(0编辑  收藏  举报