UVALive 7045 Last Defence(2014西安赛区现场赛)
2015-10-09 14:53
344 查看
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,求出这个序列中出现不同数字的个数。
(我也不太明白为啥这样写,目测是根据观察得出的结论)
• 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; }
相关文章推荐
- 动态执行JS
- JS获取本周、本季度、本月、上月的开端日期、停止日期
- javascript替换手机号中间4位
- JavaScript子窗口调用父窗口变量和函数的方法
- JQuery EasyUI DataGrid 纵向(转置)表格插件 TransposedView
- 纯CSS实现气泡框
- javascript格式化日期格式
- jsp,servlet,javascript,xml,html之间的关系
- JQuery报错Uncaught TypeError: Illegal invocation
- 201510091346_《JavaScript的事件模型——createEvent、initEvent、dispatchEvent》
- JSON语法五大要素图文介绍
- Anjularjs控制器通信
- jQuery的Deferred对象测试笔记以及源码分析
- JQuery实现在页面中央弹出带有动画透明的弹出层
- $().each() 与 $.each()区别,以及 jquery ajax 应用
- javascript之数组操作
- Vimeo 有趣的招聘前端人才的手段
- jQuery学习日记-------第一篇(jQuery介绍)
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
- Multiple dex files define Lcom/google/gson/JsonSerializer;