You have two variables a and b. Consider the following sequence of actions performed with these variables:

  1. If a = 0 or b = 0, end the process. Otherwise, go to step 2;
  2. If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 3;
  3. If b ≥ 2·a, then set the value of b to b - 2·a, and repeat step 1. Otherwise, end the process.

Initially the values of a and b are positive integers, and so the process will be finite.

You have to determine the values of a and b after the process ends.

Input

The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018). n is the initial value of variable a, and m is the initial value of variable b.

Output

Print two integers — the values of a and b after the end of the process.

Examples
input

Copy
12 5
output
0 1
input

Copy
31 12
output
7 12
Note

Explanations to the samples:

  1. a = 12, b = 5  a = 2, b = 5  a = 2, b = 1  a = 0, b = 1;
  2. a = 31, b = 12  a = 7, b = 12.

官方题解:

The answer can be calculated very easy by Euclid algorithm (which is described in the problem statement), but all subtractions will be replaced by taking by modulo.

题意:

Euclid算法,和题意一样,我最开始是按照题目给的流程按部就班的写,a,b的范围为10^18,要开long long,但是在text3 10^18 7就TLE了。

于是后面看到大佬的代码,以及官方题解,发现-=的话,效率会很低,改成%=即可。

代码:

#include<bits/stdc++.h>

long long a, b;
int main(){ scanf("%lld %lld", &a, &b);
while(a && b){
if(a>=2*b) a%= 2*b;
else if(b>=2*a) b%=2*a;
else break;
}
printf("%lld %lld", a, b);
}

Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)的更多相关文章

  1. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  2. Educational Codeforces Round 39

    Educational Codeforces Round 39  D. Timetable 令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间 转移就是枚举第\ ...

  3. #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

    2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...

  4. Educational Codeforces Round 68 Editorial

    题目链接:http://codeforces.com/contest/1194                                            A.Remove a Progre ...

  5. Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

    https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  6. Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number

    题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...

  7. Educational Codeforces Round 53 Editorial

    After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...

  8. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D

    D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

随机推荐

  1. 【Java基础总结】泛型

    泛型实现了参数化类型的概念,使代码可以应用于多种类型. 1. 泛型类 声明的泛型类型静态方法不能使用 class Tools<T>{ private T t; public void se ...

  2. EF 使用lambda表达式 更新一对多数据时报错

    1.需求  更新一对多表中的附表数据,表结构如下: 2.思路 个人觉得一个个去对比关联的附表数据是删除还是添加比较麻烦,就直接清空主表关联的附表,然后重新建立关联关系. 3.弊端 如果附表(前提是附表 ...

  3. 原生javascript 元素依次掉落及上升

    一.实现原理: ① 通过onoff开关,判断元素是往下走 还是往上走,并在每次清除定时器后,把onoff 设为 !onoff,以便下次点击做判断 ②move函数的运用 二.代码 <!DOCTYP ...

  4. python类型检查和类型转换

    类型检查 type()用来检查值的类型 (整型.浮点型.布尔值.字符串.空值) 该函数会将检查的结果作为返回值返回,可以通过变量来接收函数的返回值 print(type(1)) # <class ...

  5. 数据量不足,MedicalNet 如何助力医疗影像 AI 突破瓶颈?

    ​导读 |近日,云+社区技术沙龙“腾讯开源技术”圆满落幕.本次沙龙邀请了多位腾讯技术专家,深度揭秘了腾讯开源项目TencentOS tiny.TubeMQ.Kona JDK.TARS以及Medical ...

  6. 关于程序员须知的 linux 基础

    我在 github 上新建了一个仓库 日问,每天一道面试题,有关前端,后端,devops以及软技能,促进职业成长,敲开大厂之门,欢迎交流 并且记录我的面试经验 17年面试记(阿里百度美团头条小米滴滴) ...

  7. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  8. Win10永久版低价购买及激活工具使用说明

    目录 按 发展历程 用户界面 激活工具 按 Windows 10是由美国微软公司开发的应用于计算机和平板电脑的操作系统,于2015年7月29日发布正式版. Windows 10操作系统在易用性和安全性 ...

  9. TryCatchFinallyReturn

    public class TryCatchFinallyReturnTest { public int test(){ try { int i=1; int j=2/i; return 1; }cat ...

  10. Python Selenium定位元素常用解决办法

       在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况(报selenium.common.exceptions.NoSuchElementException), ...