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 ...
随机推荐
- linux命令行操作基本知识
乱七八糟的命令 . 表示当前目录 .. 表示上一级目录 ls 显示文件 -l 列表 -a 隐藏文件 -h 文件大小人性化显示 gedit 自带文本编辑器 subl 打开sublime > 重定向 ...
- 【MySQL】资源列表
1.使用yum方式安装MySQL https://blog.csdn.net/zl570932980/article/details/78934601 2.安装Xtrabackup备份工具 https ...
- loj2053 「HNOI2016」大数
ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...
- Hyper-V:利用差异磁盘安装多个Win2008
签于成本的原因,在学习了解一项新的技术或是产品时,在没有部署到生产环境之中前,大家都会选择在虚拟机来搭建一套实验环境.但如何快速搭建呢?如何节省磁盘空间呢? 说到此都不得不说下Hyper-V的差异磁盘 ...
- 14、响应式布局和BootStrap 全局CSS样式知识点总结-part1
1.什么是响应式布局 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,这个概念是为解决移动互联网浏览而诞生的. 简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一 ...
- Java的移位运算符
1.左移运算符:<< 丢弃左边指定位数,右边补0. 注意: 当int类型进行左移操作时,左移位数大于等于32位操作时,会先求余(%)后再进行左移操作.也就是说左移32位相当于不进行移位操作 ...
- c++ 中double与string之间的转换,char *
运行代码为 /* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> //#inc ...
- angular2多组件通信流程图
知识点1:组件属性的双向绑定,需要在属性+Change 作为Output方法返回即可. 知识点2:更新子组件的值,不会引起output触发,父组件不会更新绑定的值 知识点3:属性的双向绑定,只会子组件 ...
- 【bzoj4390】[Usaco2015 dec]Max Flow LCA
题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in h ...
- [AGC004D] Teleporter [贪心]
题面: 传送门 思路: 分析可知,这道题中的图是一个环套内向树,首都在环上 首先有一个结论:当首都的出边指向首都时,一定最优(不然首都出发可能无法按时到达首都)(可以按时到达的情况也一定有到不了的) ...