2014 ACM-ICPC - K:Last Defence
发布日期:2021-07-01 00:14:57
浏览次数:2
分类:技术文章
本文共 1226 字,大约阅读时间需要 4 分钟。
题目链接:
Time limit 1000 ms Memory limit 262144 kBDescription
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.
Sample Input
2
7 4 3 5
Sample Output
Case #1: 6
Case #2: 5
Problem solving:
找规律,模拟演算过程。例如a = S0 = 10, b = s1 = 3;
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 重新循环上述操作。#includelong 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;}
转载地址:https://lzyws739307453.blog.csdn.net/article/details/84240262 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月19日 06时40分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用redis实现订阅功能
2019-05-01
URL特殊字符转码
2019-05-01
对称加密整个过程
2019-05-01
java内存模型
2019-05-01
volatile关键字
2019-05-01
tomcat_关闭
2019-05-01
Servlet_快速入门
2019-05-01
Servlet_生命周期方法
2019-05-01
Servlet_体系结构
2019-05-01
Servlet_urlpartten配置
2019-05-01
Request_原理
2019-05-01
Request_继承体系
2019-05-01
前端权限控制:获取用户信息接口构造数据
2019-05-01
有状态服务和无状态服务
2019-05-01
七牛云存储:断点续传
2019-05-01
字节流复制文本文件【应用】
2019-05-01
字节流复制图片
2019-05-01
其他数字摘要算法实现
2019-05-01
私钥加密私钥解密
2019-05-01
锁的释放流程-ReentrantLock.unlock
2019-05-01