Under construction

Still being built. Things may change

Arithmetic and Geometric Sequences

Time limit2sMemory limit512 MB

Summary
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 AA has general term a+b×xa + b \times x and the geometric sequence GG has general term c×dyc \times d^y, where xx and yy are non-negative integers.

Given aa, bb, cc, dd, and uu, write a program that counts the integers from 11 to uu that belong to AA or to GG. 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 aa, bb, cc, dd, and uu, separated by spaces. (1≤a,b,c,u≤10121 \le a, b, c, u \le 10^{12}, 1≤d≤1051 \le d \le 10^5)

Output

Print on the first line how many integers between 11 and uu inclusive belong to the arithmetic sequence AA or to the geometric sequence GG.

Note

For a=452a = 452, b=24b = 24, c=4c = 4, d=5d = 5, and u=600u = 600, the numbers that satisfy the condition are 44, 2020, 100100, 452452, 476476, 500500, 524524, 548548, 572572, 596596.

Examples4

  1. Example 1

    Input
    1 1 1 2 1000
    
    Expected output
    1000
    
  2. Example 2

    Input
    3 3 1 2 1000
    
    Expected output
    343
    
  3. Example 3

    Input
    40 77 40 100000 40
    
    Expected output
    1
    
  4. Example 4

    Input
    452 24 4 5 600
    
    Expected output
    10