Pyramid Sequence
Time limit1sMemory limit128 MB
Count the distinct pairs formed at matching positions of two repeating pyramid sequences of heights N and M.
- Level
Medium7 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
A pyramid sequence of height is , so its first elements repeat forever. A pyramid sequence of height 1 is 1 repeated forever.
Given two natural numbers and , pair up the elements that sit at the same position in the pyramid sequence of height and the pyramid sequence of height . Write a program that counts how many different pairs appear.
For and the two sequences start like this.
- 1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 1
- 1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 3, 2, 1
The different pairs are (1,1), (2,2), (3,3), (2,4), (1,3), (3,1), so there are 6 of them.
Input
The first line contains two natural numbers and separated by a space. ()
Output
Print the number of different pairs.
Hint
For and the two sequences start like this.
- 1, 2, 3, 2, 1, 2, 3, 2, 1
- 1, 2, 3, 4, 5, 4, 3, 2, 1
The different pairs are (1,1), (2,2), (3,3), (2,4), (1,5), so there are 5 of them.