Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)
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.
官方题解:
The answer can be calculated very easy by Euclid algorithm (which is described in the problem statement), but all subtractions will be replaced by taking by modulo.
题意:
Euclid算法,和题意一样,我最开始是按照题目给的流程按部就班的写,a,b的范围为10^18,要开long long,但是在text3 10^18 7就TLE了。
于是后面看到大佬的代码,以及官方题解,发现-=的话,效率会很低,改成%=即可。
代码:
#include<bits/stdc++.h>
long long a, b;
int main(){
scanf("%lld %lld", &a, &b);
while(a && b){
if(a>=2*b) a%= 2*b;
else if(b>=2*a) b%=2*a;
else break;
}
printf("%lld %lld", a, b);
}
Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)的更多相关文章
- 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
Educational Codeforces Round 39 D. Timetable 令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间 转移就是枚举第\ ...
- #分组背包 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 68 Editorial
题目链接:http://codeforces.com/contest/1194 A.Remove a Progre ...
- 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 取模也是一样的,就当多减几次. 在欧几里得最初的 ...
- 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 开始 ...
- Educational Codeforces Round 53 Editorial
After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...
- 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 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
随机推荐
- .NET C# 红包生成算法,可设置红包总额和数量,可限制最大最小红包
很多场景算红包的要求:根本问题就是指定的钱,指定的个数,红包发完,钱不剩余,最小红包1分钱,最大也需要限制. 原理:割绳子算法:每次都取最大值为总绳长的随机值,最后将其排序,计算每两个的差值,总差值即 ...
- 【转】C#中base关键字的几种用法:base()
转:https://blog.csdn.net/cplvfx/article/details/82982862 base其实最大的使用地方在面相对象开发的多态性上,base可以完成创建派生类实例时调用 ...
- a标签属性href值为#和javasrcript:void(0)的区别
当我们需要一个空链接时,通常有两种方法: <a href="#">这个一个空链接</a> <a href="javascript:void( ...
- Download EditPlus Text Editor
突然发现EditPlus还是很强大的,很好用,破解也很方便,有个牛人做了在线生成验证码,只能说服!! 下边把官网的最新下载地址贴出,当然还有在线生成验证码喽. EditPlus Text Editor ...
- 悄摸直播(三)—— 搭建rtmp服务器(smart_rtmpd - rtmp服务器搭建)
悄摸直播 -- javaCV实现本机摄像头画面远程直播 搭建rtmp服务器 一.素材 rtmp服务器:smart_rtmpd ffmpeg工具:ffmpeg.exe 二.搭建 1.下载smart_rt ...
- 图解kubernetes调度器ScheduleAlgorithm核心实现学习框架设计
ScheduleAlgorithm是一个接口负责为pod选择一个合适的node节点,本节主要解析如何实现一个可扩展.可配置的通用算法框架来实现通用调度,如何进行算法的统一注册和构建,如何进行metad ...
- python关系(比较)运算符
关系运算符 就是 比较运算符 a.对象的值进行比较 数字间的比较运算符连着使用: 数字与True.False的比较True 表示 1 , False 表示 0 数字与字符串的比较(不能比较) 字符串间 ...
- Redis 通配符批量删除key
问题: 线上有部分的redis key需要清理. 一. 由于Keys模糊匹配,请大家在实际运用的时候忽略掉.因为Keys会引发Redis锁,并且增加Redis的CPU占用,情况是很恶劣的, 官网说明如 ...
- 从Main读取appsetting
using System; using System.Configuration; using Newtonsoft.Json.Linq; using System.Net.Http; using S ...
- 「 深入浅出 」集合Set
系列文章 「 深入浅出 」集合List 「 深入浅出 」java集合Collection和Map Set继承自Collection接口,不能包含有重复元素.本篇文章主要讲Set中三个比较重要的实现类: ...