The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets running east to west bound them. A helicopter took off in the most southwestern crossroads and flew along the straight line to the most northeastern crossroads. How many blocks did it fly above?
Note. A block is a square of minimum area (without its borders).

Input

The input contains N and M separated by one or more spaces. 1 < NM < 32000.

Output

The number of blocks the helicopter flew above.
 
思路:令n = n -1, m = m - 1
考虑n或m只有1的情况,不妨设m=1,那么图形就变成了一个n个小正方体拼在一起的长方体,显然答案为n,因为灰机穿过了n个街区,我们可以认为,每穿过一条竖线,就会多穿过一个街区(包括第一条竖线,不包括最后一条竖线)。
那么当n,m>1的时候,也是每穿过一条竖线,多穿过一个街区,每穿过一条横线,多穿过一个街区。但是答案却不是n+m。
因为当灰机穿过一个整点(横纵坐标均为整数)的时候,同时穿过横线和竖线,多穿过的街区是一样的。
对于灰机的路线来讲,穿过的整点数为gcd(n, m)(不算最后一个)
第一个穿过的整点为(1,1),第二个为(n/gcd, m/gcd),第三个为(n/gcd*2, m/gcd*2)……最后一个为(n/gcd*(gcd-1), m/gcd*(gcd-1))。一共gcd个。
那么n+m减掉穿过整点的时候重复加上的街区,则答案为n+m-gcd(n,m)
 
代码(0.171S):
 from fractions import gcd
n, m = map(int, raw_input().split(' '))
print n + m - gcd(n - 1, m - 1) - 2

URAL 1139 City Blocks(数论)的更多相关文章

  1. URAL 1133 Fibonacci Sequence(数论)

    题目链接 题意 :给你第 i 项的值fi,第 j 项的值是 fj 让你求第n项的值,这个数列满足斐波那契的性质,每一项的值是前两项的值得和. 思路 :知道了第 i 项第j项,而且还知道了每个数的范围, ...

  2. Ural 2003: Simple Magic(数论&思维)

    Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is e ...

  3. HDU 2722 Here We Go(relians) Again (spfa)

    Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/ ...

  4. Here We Go(relians) Again

    Here We Go(relians) Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  5. (28)A practical way to help the homeless find work and safety

    https://www.ted.com/talks/richard_j_berry_a_practical_way_to_help_the_homeless_find_work_and_safety/ ...

  6. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  7. HDU 2722 Here We Go(relians) Again (最短路)

    题目链接 Problem Description The Gorelians are a warlike race that travel the universe conquering new wo ...

  8. hdu 2722 Here We Go(relians) Again (最短路径)

    Here We Go(relians) Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  9. 【CF MEMSQL 3.0 B. Lazy Security Guard】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. C#Form窗体通过代码改变尺寸

    通过Size属性不能得到正确的窗体尺寸, 怎么办? 还需要设置 MaximumSize 属性和你的 size属性尺寸一样. this.FormBorderStyle = FormBorderStyle ...

  2. nodejs express测试

    1.页面请求 app.get('/list_user', function (req, res) { console.log("/list_user GET 请求"); //res ...

  3. 汇编查看StackFrame栈帧

    INCLUDE Irvine32.inc myProc PROTO, x:DWORD, y:DWORD .data .code main proc mov eax,0EAEAEAEAh mov ebx ...

  4. JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台

    JeeSite本身是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓 ...

  5. 几个简单的html+css+js题目

    1.页面中有一图片,请在下划线处添加代码能够实现隐藏该图片的功能 <img id="pic" src="door.jpg" width="200 ...

  6. pointer-events:none;穿透属性

    从属性字面上看可以理解为:手势时间无效. 当我们在ios下想复制一段文字是,不知道原因导致一些莫名的怪异现象:总是无法复制文字,却意外的发现无论怎么着复制的始终都是图片时, 当我们在一个半透明遮罩上想 ...

  7. 多选列表Select之双击删除与添加Demo

    双击任一Select控件,查看效果: srcA srcC srcB targetC targetB targetA   源码: <html> <head> <script ...

  8. Swift-11-协议(Protocols)

    协议定义了一个蓝图,规定了用来实现某一特定工作或者功能所必须的方法和属性.类.结构体或者枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能满足协议要求的类型被称为遵循confor ...

  9. Java学习-018-EXCEL 文件写入实例源代码

    众所周知,EXCEL 也是软件测试开发过程中,常用的数据文件导入导出时的类型文件之一,此文主要讲述如何通过 EXCEL 文件中 Sheet 的索引(index)或者 Sheet 名称获取文件中对应 S ...

  10. 一网打尽当下NoSQL类型、适用场景及使用公司

    在过去几年,关系型数据库一直是数据持久化的唯一选择,数据工作者考虑的也只是在这些传统数据库中做筛选,比如SQL Server.Oracle或者是MySQL.甚至是做一些默认的选择,比如使用.NET的一 ...