Diagonal Across Tiles
Time limit1sMemory limit128 MB
Given a grid of x by y unit tiles, count how many tiles a diagonal from corner to corner passes through using the gcd formula.
- Level
Easy3 of 10
- Topics
- Math, Number theory
- Solved
- No attempts yet
Problem
Unit square tiles with side length 1cm are used to fill an xcm by ycm rectangular wall without gaps. x and y are positive integers.
A diagonal is drawn from one corner of the rectangle to the opposite corner. Among all x * y tiles, determine how many tiles the diagonal passes through.
Input
The first line contains the rectangle width x and height y. x and y are positive integers no greater than 1,000,000,000, and there is at least one space between them.
Output
Print the number of tiles that the diagonal passes through.