URAL 1139 City Blocks(数论)
2014-03-01 18:43
387 查看
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).
题目大意:http://www.nocow.cn/index.php/Translate:URAL/1139
思路:令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):
View Code
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 < N, M < 32000.Output
The number of blocks the helicopter flew above.题目大意:http://www.nocow.cn/index.php/Translate:URAL/1139
思路:令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
View Code
相关文章推荐
- URAL 1139. City Blocks (数论)
- Interesting Numbers URAL - 2070 数论
- 数论 ural 1356. Something Easier
- ural 2003. Simple Magic 数论 因数分解
- ural 1356. Something Easier(数论,哥德巴赫猜想)
- URAL 2070 Interesting Numbers(数论)
- URAL 1430. Crime and Punishment(数论)
- URAL 1049 Brave balloonists【数论】
- 1139. City Blocks
- ural 1049. Brave Balloonists(标准分解式,数论)
- [求解二次剩余 数论技巧 随机化] Ural 1132 Square Root
- ural 2070 Interesting Numbers(数论基础)
- Timus 1139. City Blocks 题解
- 【数论】ural1091
- URAL 1355. Bald Spot Revisited(数论)
- Ural1049 基础数论
- URAL 1104(数论)
- Ural2102:Michael and Cryptography(数论&素数)
- URAL 1876 Centipede's Morning[数论]
- URAL1014 - Product of Digits - 数论