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的更多相关文章

  1. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  2. [UCSD白板题] Least Common Multiple

    Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...

  3. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  4. CCPC2018 桂林 G "Greatest Common Divisor"(数学)

    UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...

  5. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  6. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  7. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  8. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

  9. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

随机推荐

  1. Altium Designer15 卡在登陆界面解决办法:

    Altium Designer15 卡在登陆界面解决办法: 在我的电脑系统盘中找到下面目录(注:如果看不到,需要取消隐藏文件选项.) C:\Documents and Settings\Adminis ...

  2. lua 代码风格

    参考  http://www.kancloud.cn/kancloud/lua_style_guide/66327 1.命名 1.命名法:小驼峰命名法,大驼峰命名法(Pascal命名法),小下划线命名 ...

  3. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans

    Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...

  4. Android WIFI 分析(二)

    本文介绍Wifi 分析线路二:在Setting中打开WiFi功能.扫描网络以及连接网络的流程. WifiSettings 无线网络设置界面 WifiEnabler 相当于无线网络设置开关 WifiDi ...

  5. mysql中的游标使用案例

    DELIMITER $$ DROP PROCEDURE IF EXISTS `curTest`$$ CREATE PROCEDURE curTest(IN _myId INT) BEGIN DECLA ...

  6. Entity Framework Code First数据库自动更新

    EF的Code First方式允许你先写Model,再通过Model生成数据库和表. 具体步骤如下: 1.建项目 2.在model文件夹中,添加一个派生自DbContext的类,和一些Model类. ...

  7. Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用

    如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...

  8. Java程序设计笔记

    程序:编写Java程序,此程序从命令行接收多个数字,求和之后输出结果. 设计思想:首先在程序中设置关于参数个数的长度的公式,用.length公式读出用户所设置的参数的个数,参数默认为字符串类型,利用强 ...

  9. mysql定时器Events

    MySQL定时器Events 一.背景 我们MySQL的表A的数据量已经达到1.6亿,由于一些历史原因,需要把表A的数据转移到一个新表B,但是因为这是线上产品,所以宕机时间需要尽量的短,在不影响数据持 ...

  10. AngularJs自定义指令详解(10) - 执行次序

    代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...