• Last Defence


    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

     1 #include <iostream>
     2 using namespace std;
     3 long long a, b, ans;
     4 int nCase, cCase;
     5 long long calc(long long a, long long b)
     6 {
     7     long long ret = 0;
     8     while (b)
     9     {
    10         long long t = b;
    11         ret += a / b;
    12         b = a % b;
    13         a = t;
    14     }
    15     return ret + 1;
    16 }
    17 int main()
    18 {
    19     //ios::sync_with_stdio(false);
    20     cin >> nCase;
    21     while (nCase--)
    22     {
    23         cin >> a >> b;
    24         if (a == 0 && b == 0)
    25         {
    26             ans = 1;
    27         }
    28         else if (a == 0 || b == 0)
    29         {
    30             ans = 2;
    31         }
    32         else
    33         {
    34             ans = calc(a, b);
    35         }
    36         cout << "Case #" << ++cCase << ": " << ans << endl;
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    [模板] 文艺平衡树
    [模板]平衡树splay
    [ZJOI2015]诸神眷顾的幻想乡
    [HAOI2016]找相同字符
    SP8093 JZPGYZ
    SP1812 LCS2
    SP1811 LCS
    AHOI2013 差异
    TJOI2015 弦论
    工艺(SAM)
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4864816.html
Copyright © 2020-2023  润新知