https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95

取模也是一样的,就当多减几次.

在欧几里得最初的描述中,商和余数是通过连续的减法来计算的,即从rk−2中不断减去rk−1直到小于rk−1。一个更高效的做法是使用整数除法和模除来计算商和余数:

rk rk−2 mod rk−1
在欧几里得定义的减法版本,取余运算被减法替换
while (b!=0)
{
if (a>b) a=a-b;
else b=b-a;
} //结果是a

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.
 
 

[题意]:看算法步骤
[分析]:类似欧几里得算法,取余运算替换减法运算,效率更高
[代码]:
 
#include<bits/stdc++.h>
using namespace std;
#define z(i) (1<<i)
#define g(x,y) (3*((x-1)/3)+(y-1)/3+1)
#define LL long long
int read()
{
int _=,___=;char __=getchar();
while(__<''||__>''){if(__=='-')___=-;__=getchar();}
while(__>=''&&__<=''){_=_*+__-'';__=getchar();}
return _*___;
}
int main()
{
LL a,b;
cin>>a>>b;
while(a&&b){
if(a>=*b) a%=*b;
else if(b>=*a) b%=*a;
else break;
}
cout<<a<<" "<<b<<endl;
return ;
}

Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]的更多相关文章

  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 (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 ...

  3. 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 开始 ...

  4. 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 ...

  5. Educational Codeforces Round 74 (Rated for Div. 2) A. Prime Subtraction

    链接: https://codeforces.com/contest/1238/problem/A 题意: You are given two integers x and y (it is guar ...

  6. Educational Codeforces Round 88 (Rated for Div. 2) E. Modular Stability(数论)

    题目链接:https://codeforces.com/contest/1359/problem/E 题意 有一大小为 $k$ 的数组,每个元素的值在 $[1,n]$ 间,若元素间两两不等,问有多少数 ...

  7. Educational Codeforces Round 79 (Rated for Div. 2) - D. Santa's Bot(数论)

    题意:有$n$个孩子,第$i$个孩子有$k[i]$件想要的礼物,第$j$个礼物为$a[i][j]$,现在随机挑一个孩子,从他想要的礼物里面随机挑一个,然后送给另一个孩子$($这个孩子可以和第一个孩子是 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. 图学java基础篇之并发

    概述 并发处理本身就是编程开发重点之一,同时内容也很繁杂,从底层指令处理到上层应用开发都要涉及,也是最容易出问题的地方.这块知识也是评价一个开发人员水平的重要指标,本人自认为现在也只是学其皮毛,因此本 ...

  2. TCP的运输连接管理

    TCP的运输连接管理 TCP是面向连接的协议,有三个阶段:连接建立.数据传送 和 连接释放.运输连接的管理就是使运输连接的简历和释放都能正常地进行. 在TCP连接建立过程中要解决一下三个问题: 1.  ...

  3. day21&22&23:线程、进程、协程

    1.程序工作原理 进程的限制:每一个时刻只能有一个线程来工作.多进程的优点:同时利用多个cpu,能够同时进行多个操作.缺点:对内存消耗比较高当进程数多于cpu数量的时候会导致不能被调用,进程不是越多越 ...

  4. luogu3810 【模板】三维偏序(陌上花开)

    ref1 ref2 ref3 ref4 #include <algorithm> #include <iostream> #include <cstdio> usi ...

  5. Gpfixup

    Updated: April 17, 2012 Applies To: Windows Server 2003, Windows Vista, Windows Server 2008, Windows ...

  6. 友推在Android 实现微信等分享代码的常见问题

    介绍,最近 做了一个项目,需要集成分享功能.果断选择 友推. 集成过程,参考友推官方提供的集成文档即可 废话不多说,主要说一下自己在集成过程中遇到的一些问题,主要有两个: 问题1. 引入youtui- ...

  7. 61、请求数据进行gizp压缩

    1.请求时进行头部处理 /** * 设置通用消息头 * * @param request */ public void setHeader(HttpUriRequest request) { // r ...

  8. leetcode 【 Search in Rotated Sorted Array II 】python 实现

    题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/42545 ...

  9. STL学习笔记8 -- 函数对象

    重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过“对象名+(参数列表)”的方式使用一个类对象,如果 ...

  10. Python学习-day14-HTML

    以下博客为转载 http://www.cnblogs.com/evilliu/p/5750539.html 一:HTML(HyperText Markup Language)介绍 超文本标记语言,标准 ...