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. ASP.NET一些公共方法commTools

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  2. 文件批量上传的工具,要实现暂停继续、断点续传等功能(使用QtNetwork和QHttpMultiPart,和定时器检查超时)

    最近在做一个文件批量上传的工具,要实现暂停继续.断点续传等功能.利用Qt自带的QtNetwork模块,完成这些需求并没有费多少周章,主要思路就是将文件分块,然后用while循环依次传输.具体实现代码比 ...

  3. Delphi HOOK示例

    本应用程序的Hook: unit UFrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...

  4. pod setup》error: RPC failed; result=18, HTTP code = 200

    Try reducing the postBuffer size in the remote repository config. Follow the steps below Go to remot ...

  5. 简易自定义下拉菜单 与简易默认下拉html片段

    简易自定义下拉选择 html片段 html: <div class="select_box province"> <div class="selecte ...

  6. Ubuntu deb包使用

    deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...

  7. App 上线流程

    http://www.cocoachina.com/bbs/read.php?tid=330302

  8. 利用HTML和JS制作隔行换背景颜色的表格

    1.源代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  9. undefined和void

    1.undefined undefined在js中并不是关键字/保留字,因此在IE5.5~8中可以对undefined赋值,但是在IE9以上,对其赋值是无效的 <script> var a ...

  10. Sqlcompletefree

    Sqlcompletefree SQLSERVER中SQL代码自动提示填充,自动格式化SQL语句工具Sqlcompletefree.方便开发操作.