Jealous Numbers

Time limit3sMemory limit256 MB

Summary
Count integers in a huge range [a, b] whose exponent of prime p in factorization exceeds that of prime q.
Level

Medium6 of 10

Topics
Math, Number theory, Binary search
Solved
No attempts yet

Problem

In Numberland the prime pp envies the prime qq. To settle the rivalry we count how often pp "wins" over qq within a range.

For a positive integer nn and a prime xx, let α(n,x)\alpha(n, x) be the largest integer kk such that xkx^k divides nn; equivalently, α(n,x)\alpha(n, x) is the exponent of xx in the prime factorization of nn.

We say that nn is pp-dominating over qq when α(n,p)>α(n,q)\alpha(n, p) > \alpha(n, q).

Given aa, bb, pp, and qq, count how many integers nn with a≤n≤ba \le n \le b are pp-dominating over qq.

Input

A single line with four integers aa, bb, pp, and qq (1≤a≤b≤10181 \le a \le b \le 10^{18}; 2≤p,q≤1092 \le p, q \le 10^9; p≠qp \ne q; both pp and qq are prime).

Output

Print one integer: the number of integers nn in [a,b][a, b] that are pp-dominating over qq.

Hint

For the sample, the integers in [1,20][1, 20] that are 33-dominating over 22 are 33, 99, 1515, and 1818.

Examples3

  1. Example 1

    Input
    1 20 3 2
    
    Expected output
    4
    
  2. Example 2

    Input
    10 20 3 2
    
    Expected output
    2
    
  3. Example 3

    Input
    9 9 3 2
    
    Expected output
    1