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 ...
随机推荐
- Leetcode 105. 从前序与中序遍历序列构造二叉树
题目链接 题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder ...
- Diycode开源项目 NotificationActivity
1.NotificationActivity预览以及布局详解 1.1.首先看一下通知的具体页面. 1.2.然后是布局代码==>activity_fragment.xml <LinearLa ...
- luogu3810 【模板】三维偏序(陌上花开)
ref1 ref2 ref3 ref4 #include <algorithm> #include <iostream> #include <cstdio> usi ...
- Python框架之Django学习笔记(六)
模板 上篇博文学习了动态视图,但是,视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. def current_datetime(request): now = ...
- 【Longest Substring Without Repeating Characters】cpp
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- X-UA-Compatible设置IE浏览器兼容模式
文件兼容性用来告诉IE,让它如何来编译你的网页. 指定 文件兼容性模式 以下是指定为Emulate IE7 mode 兼容性范例. <html> <head> <!-- ...
- ABP 未能加载文件或程序集“System.ComponentModel.Annota, Version=4.2.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。
切换System.ComponentModel.Annotations版本到4.4.1 重新编译即可
- 【转】behave行为树学习使用第一天
最近在学习使用行为树做AI,决定把学到的贡献出来,抛砖引玉,希望可以认识到更多大牛 -- 首先我们了解下什么是行为树和为什么要使用行为树. 在我们项目中如果需要做一个AI敌人,比如做一个手游 某民 ...
- jquery对象与js对象
对于已经是一个 DOM 对象,只需要用 $() 把DOM对象包装起来,就可以获得一个 jQuery 对象了,使用[index]和.get(index)可以转为DOM对象 jQuery 对象是通过 jQ ...
- FreeBSD利用 ports 來安裝軟體
FreeBSD利用 ports 來安裝軟體 利用 ports 來安裝軟體 FreeBSD 的 ports 就是別人已經編譯過,安裝測試沒問題了,他們將軟體編譯時所需的組態設定.編譯程序及安裝程序, ...