B. Weird Subtraction Process
 
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

代码:

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cmath>
5 #include<algorithm>
6 #include<cstdlib>
7 using namespace std;
8 typedef long long ll;
9 const int maxn=1e5+10;
10 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
11 int main(){
12 ll a,b;
13 ios;
14 cin>>a>>b;
15 while(1){
16 if(a==0||b==0) break;
17 if(a>=2*b){
18 a=a%(2*b);
19 }
20 else if(b>=2*a){
21 b=b%(2*a);
22 }
23 else break;
24 }
25 cout<<a<<" "<<b<<endl;
26 }

Codeforces 946 B.Weird Subtraction Process的更多相关文章

  1. 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 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  2. CF946B Weird Subtraction Process 题解

    Content 有两个数 \(a,b\),执行如下操作: 如果 \(a,b\) 中有一个数是 \(0\),结束操作,否则跳到操作 \(2\). 如果 \(a\geqslant 2b\),那么 \(a\ ...

  3. [codeforces 293]A. Weird Game

    [codeforces 293]A. Weird Game 试题描述 Yaroslav, Andrey and Roman can play cubes for hours and hours. Bu ...

  4. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  5. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  6. Educational Codeforces Round 11——C. Hard Process(YY)

    C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. 【codeforces 789D】Weird journey

    [题目链接]:http://codeforces.com/problemset/problem/789/D [题意] 给你n个点,m条边; 可能会有自环 问你有没有经过某两条边各一次,然后剩余m-2条 ...

  8. 【codeforces 779B】Weird Rounding

    [题目链接]:http://codeforces.com/contest/779/problem/B [题意] 问你要删掉几个数字才能让原来的数字能够被10^k整除; [题解] /* 数字的长度不大; ...

  9. Codeforces 946 D.Timetable-数据处理+动态规划(分组背包) 处理炸裂

    花了两个晚上来搞这道题. 第一个晚上想思路和写代码,第二个晚上调试. 然而还是菜,一直调不对,我的队友是Debug小能手呀(真的是无敌,哈哈,两个人一会就改好了) D. Timetable   tim ...

随机推荐

  1. Ubuntux下简单设置vim

    我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...

  2. 数据库储存session信息代码

    今天给大家上一段代码,数据库存储session信息,你只需要将下面这段代码放到session文件中,然后再session_start()的地方引入sessiong文件就行啦,当然你就不用再写sessi ...

  3. 多线程辅助类之CountDownLatch(三)

    CountDownLatch信号灯是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.它可以实现多线程的同步互斥功能,和wait和notify方法实现功能类似,具体 ...

  4. CF 510b Fox And Two Dots

    Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on ...

  5. LightOj:1030-Discovering Gold(期望dp模板)

    传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...

  6. poj 3190 奶牛挤奶问题 贪心算法

    题意:奶牛挤奶问题,每只奶牛在[a,b]的时间内挤奶,要求挤奶的过程中不能不打扰,且只能自己一个人独享挤奶的机器.问最少需要多少个挤奶的机器? 思路: 对奶牛挤奶开始的时间从小到大开始排序. 将正在工 ...

  7. Linux学习-灾难复原的考虑

    硬件损毁,且具有完整备份的数据时 由于是硬件损毁,所以我们不需要考虑系统软件的不稳定问题,所以可以直接将完整的系统复原回去 即可. 由于软件的问题产生的被攻破资安事件 由于系统的损毁是因为被攻击,此时 ...

  8. (转)全网最!详!细!tarjan算法讲解

    byhttp://www.cnblogs.com/uncle-lu/p/5876729.html 全网最详细tarjan算法讲解,我不敢说别的.反正其他tarjan算法讲解,我看了半天才看懂.我写的这 ...

  9. pycharm下搭建django开发环境

    在一次偶然的机会中,了解到万精油语言python,发现其流行程度发展迅速,于是也开始学习起来,正题. 1.安装python,查阅一些相关的资料及周边的开发工具,我选择python2(2.7.11),注 ...

  10. Selenium WebDriver- 显式等待

    推荐使用显示等待,元素出现就不会等待而继续执行了.节省时间. #encoding=utf-8 import unittest import time from selenium import webd ...