[UCSD白板题] Greatest Common Divisor
Problem Introduction
The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) and \(b\) (which are not both equal to 0) is the greatest integer \(d\) that divides both \(a\) and \(b\).
Problem Description
Task.Given two integer \(a\) and \(b\), find their greatest common divisor.
Input Format.The two integers \(a\),\(b\) are given in the same line separated by space.
Constraints. \(1 \leq a, b \leq 2 \cdot 10^9\).
Output Format. Output \(GCD(a,b)\).
Sample 1.
Input:
18 35
Output:
1
Sample 2.
Input:
28851538 1183019
Output:
17657
Solution
# Uses python3
import sys
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
if __name__ == "__main__":
input = sys.stdin.read()
a, b = map(int, input.split())
print(gcd(a, b))
[UCSD白板题] Greatest Common Divisor的更多相关文章
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Least Common Multiple
Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...
- 2018CCPC桂林站G Greatest Common Divisor
题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...
- CCPC2018 桂林 G "Greatest Common Divisor"(数学)
UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
随机推荐
- ROW_NUMBER() OVER的用法
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) //PARTITION 分割 一.ROW_NUMBER() OVER ...
- maven的配置环境及Myeclipse部署Maven项目
1.官网下载maven>解压>配置环境变量:在path后面加上 D:\software\apache-maven-3.3.9\bin; 2.cmd/mvn -version 测试 显示版 ...
- secure boot(安全启动)下为内核模块签名
上一篇随笔中提到了如何在secure boot下安装Nvidia显卡驱动 >>上一篇随笔 如果不需要安装Nvidia显卡驱动,而且要生成密钥,可以参考>> 这篇文章 这里假设生 ...
- EF外键关联
客户里面存在客服外键 基类模型 public class ModelBase { public ModelBase() { CreateTime = DateTime.Now; } [Key] pub ...
- modelsim 中 WAVE窗口中能不能只显示变量名,而不显示路径
可以的,在wave窗口左下角有一个黑色的logo,你点击它就可以省电路径,只显示port名称,再点击就切换回来了,如图红色圈圈标记的logo,你可以试试!
- html之如何让文字两端对齐
text-align: justify;/*英文*/ text-align-last: justify;/*中英文*/ text-align-last: justify;亲测有效(chrome)
- 使用id名称和name直接获取元素
我们知道一些第三方的js库对如何快速选取html中的元素做了一些简化,貌似十分高深莫测,其实也不然.而且js本身自带了对于特殊元素的简便选取的方法,下面就为大家简单介绍下. 在html中,一般最直接的 ...
- json对象
//数组var arr=[1,2,3,3];//json对象var obj={width:'200px',height:'200px',background:'green'}; alert(obj[' ...
- Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用
如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...
- Gradle笔记系列(一)
1.Gradle概述 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的 ...