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
取模也是一样的,就当多减几次.
在欧几里得最初的描述中,商和余数是通过连续的减法来计算的,即从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
1 second
256 megabytes
standard input
standard output
You have two variables a and b. Consider the following sequence of actions performed with these variables:
- If a = 0 or b = 0, end the process. Otherwise, go to step 2;
- If a ≥ 2·b, then set the value of a to a - 2·b, and repeat step 1. Otherwise, go to step 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.
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.
Print two integers — the values of a and b after the end of the process.
12 5
0 1
31 12
7 12
Explanations to the samples:
- a = 12, b = 5 a = 2, b = 5 a = 2, b = 1 a = 0, b = 1;
- 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[数论/欧几里得算法]的更多相关文章
- 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 < ...
- #分组背包 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 ...
- 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 开始 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 88 (Rated for Div. 2) E. Modular Stability(数论)
题目链接:https://codeforces.com/contest/1359/problem/E 题意 有一大小为 $k$ 的数组,每个元素的值在 $[1,n]$ 间,若元素间两两不等,问有多少数 ...
- Educational Codeforces Round 79 (Rated for Div. 2) - D. Santa's Bot(数论)
题意:有$n$个孩子,第$i$个孩子有$k[i]$件想要的礼物,第$j$个礼物为$a[i][j]$,现在随机挑一个孩子,从他想要的礼物里面随机挑一个,然后送给另一个孩子$($这个孩子可以和第一个孩子是 ...
- 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 ...
- 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 ...
随机推荐
- 最短路径(最基础,经典的模板和思想):HDU-2544最短路
题目: 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 6228 tree 简单思维树dp
一.前言 前两天沈阳重现,经过队友提点,得到3题的成绩,但是看到这题下意识觉得题目错了,最后发现实际上是题目读错了....GG 感觉自己前所未有的愚蠢了....不过题目读对了也是一道思维题,但是很好理 ...
- 笔记-python-standard library-9.6 random
笔记-python-standard library-9.6 random 1. random source code:Lib/random.py 1.1. functions for ...
- OWINS是什么(转载)
OWIN的英文全称是Open Web Interface for .NET. 如果仅从名称上解析,可以得出这样的信息:OWIN是针对.NET平台的开放Web接口. 那Web接口是谁和谁之间的接口呢?是 ...
- Python中*和**的区别
Python中,(*)会把接收到的参数形成一个元组,而(**)则会把接收到的参数存入一个字典 我们可以看到,foo方法可以接收任意长度的参数,并把它们存入一个元组中 >>> def ...
- mysql练习题练习
1.数据库是按照原文制作的,表格结构一样具体存储的数据有些差异 原文地址:MySQL练习题 原答案地址:MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: selec ...
- Ecplise实战常用操作快捷键(更新至2018年10月8日 13:46:40)
ctrl+鼠标左键 进入/查看这个类或者方法, ctrl + t 快速类型层次结构(出现部分方法) ctrl + o 快速大 ...
- 什么是虚假唤醒 spurious wakeup
解释一下什么是虚假唤醒? 说具体的例子,比较容易说通. pthread_mutex_t lock; pthread_cond_t notempty; pthread_cond_t notfull; v ...
- iis特殊后缀文件下载404
项目中有特殊类型的文件上传到服务器,下载的时候报404,下载不下来.如后缀名为qwwq这种类型. 因为遇到过这种情况,以前都是配置mime类型好的,但是这次没有找到,到底应该配置什么mime类型,一时 ...
- 【转】DontDestroyOnLoad(Unity3D开发之五)
原文 http://blog.csdn.net/cocos2der/article/details/38320773 主题 Unity3D Unity中我们从A场景切换到B场景的时候,A场景所有对象 ...