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 ...
随机推荐
- Android 适配器 自定义
前言:最近看了几个开源项目,发现适配器这东西用的很多,一开始觉得这东西高大上,其实呢,感觉就是一个中转站,或者说是一个接口工具,将数据填充到一个视图中,几乎任何项目都会涉及到.所以今天也简单看了一下, ...
- v-model 的修饰符
1..trim 自动过滤输入内容最开始 和 最后的 空格,中间的会保留一个空格,多的会被过滤掉 2..lazy 一般情况下,在input的 v-model是一直在同步 输入的内容与显示的内容,不过再添 ...
- MFC定时关机程序的实现2-添加启动项到注册表
虽然上一篇实现了的定时关机,但是还不够完善,比如开机自动启动,然后按照配置的时间定时关机,并最小化到任务栏. 先来说开机启动怎么实现,开机启动实现的方法有好几种,比如直接在开始菜单启动项里添加一个程序 ...
- 深入学习之mysql(二)表的操作
1.表:是数据库中的存储数据的基本单位,一个表包含若干个字段和值 2.创建表: CREATE TABLE 表名称 ( 字段名1 数据库类型1 [约束条件1], 字段名2 数据库类型2 [约束条件2], ...
- win7分盘(复制)
1/10 右击“计算机”选择“管理” 2/10 打开管理之后点击“磁盘管理器”,在想要新建磁盘的分区上右击,点击“压缩卷” 3/10 在“输入压缩空间量”后面输入需要新建磁盘的大小,输入的单位为MB( ...
- PHP简单登录退出代码
PHP简单登录退出代码 登录页面login.html 负责收集用户填写的登录信息. <html> <head> <title></title> < ...
- 【bzoj5018】[Snoi2017]英雄联盟 背包dp
题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄,因此,他也只准 ...
- ICPC World Finals 2018 Problem H Single Cut of Failure
题目链接 题解视频 题解文档 解法概要: 问题可以转化为 考虑一个长为 $2n$ 的数组 $A$,$1$ 到 $n$ 这 $n$ 个整数每个恰在 $A$ 中出现 $2$ 次.判断是否存在一个长为 $n ...
- 【HDU 2087 剪花布条】
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 2017-3-01 test
三道好像都是HDU上的题QAQ 题目名称都没改,差评 T1:http://acm.hdu.edu.cn/showproblem.php?pid=5073 被卡精度了QAQ 先排一发序,然后发现最后未动 ...