/*--------------------------------------------------------------*/
/* ACM ICPC 2009-2010, NEERC                                    */
/* Northern Subregional Contest                                 */
/* St Petersburg, October 17, 2009                              */
/*--------------------------------------------------------------*/
/* Problem J. Jealous Numbers                                   */
/*                                                              */
/* Original idea         Andrew Stankevich                      */
/* Problem statement     Andrew Stankevich                      */
/* Testset               Andrew Stankevich                      */
/*--------------------------------------------------------------*/
/* Solution                                                     */
/*                                                              */
/* Author                Georgiy Korneev                        */
/*--------------------------------------------------------------*/
 
import java.util.*;
import java.io.*;

public class jealous_gk implements Runnable {
    static Scanner in;
    static PrintWriter out;

    static int MAX_P = 100;

    public void run() {
        long a = in.nextLong();
        long b = in.nextLong();
        long p = in.nextLong();
        long q = in.nextLong();

        long[][] c = new long[MAX_P][MAX_P];
        long pp = 1; 
        for (int i = 0; i < MAX_P; i++) {
            long qq = 1;
            for (int j = 0; j < MAX_P; j++) {
                c[i][j] = b / pp / qq - (a - 1) / pp / qq;
                if (qq > b / pp / q) {
                    break;
                }
                qq *= q;
            }
            if (pp > b / p) {
                break;
            }
            pp *= p;
        }

        for (int i = 0; i < MAX_P - 1; i++) {
            for (int j = 0; j < MAX_P - 1; j++) {
                c[i][j] -= c[i + 1][j] + c[i][j + 1] - c[i + 1][j + 1];
            }
        }

        long total = 0;
        for (int i = 0; i < MAX_P; i++) {
            for (int j = 0; j < i; j++) {
                total += c[i][j];
            }
        }
        out.println(total);
    }

    public static void main(String[] args) throws Exception {        
        in = new Scanner(new File("jealous.in"));
        out = new PrintWriter("jealous.out");

        new jealous_gk().run();

        in.close();
        out.close();
    }
}
