Arithmetic and Geometric Sequences
Time limit2sMemory limit512 MB
Count integers from 1 to u that lie in an arithmetic sequence or a geometric sequence, counting overlaps only once.
- Level
Medium5 of 10
- Topics
- Math, Hash map, Implementation, Brute force
- Solved
- No attempts yet
Problem
The arithmetic sequence has general term and the geometric sequence has general term , where and are non-negative integers.
Given , , , , and , write a program that counts the integers from to that belong to or to . A number that belongs to both sequences is counted as well. If the same number appears several times in a sequence, it still counts once.
Input
The first line contains , , , , and , separated by spaces. (, )
Output
Print on the first line how many integers between and inclusive belong to the arithmetic sequence or to the geometric sequence .
Note
For , , , , and , the numbers that satisfy the condition are , , , , , , , , , .