(*--------------------------------------------------------------*)
(* 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                Dmitry Shtukenberg                     *)
(*--------------------------------------------------------------*)
 
{$apptype console}

program jealous;

var a, b: int64; p, q: int64;
  res: int64;
  nn, np, nq: int64;
  dp, dq: longint;

const M: int64 = 1000000000000000000;

function power (a,b: int64): int64;
var p: int64;
begin
  p := 1;
  power := M+1;
  while b > 0 do begin
    if p > (2*M) div a then exit;
    p := p * a;
    dec (b)
  end;
  power := p  
end;

begin
  assign (input, 'jealous.in');
  assign (output, 'jealous.out');
  reset (input);
  rewrite (output);

  res := 0;
  readln (a,b,p,q);

  for dp := 1 to 65 do
    for dq := 0 to dp - 1 do begin
      np := power (p,dp);
      nq := power (q,dq);
      if (np > M) or (nq > M) or (np > (2*M) div nq) then continue;
      
      nn := np * nq;
      res := res + (b div nn - (a-1) div nn);
      if nn <= (2*M) div p then res := res - (b div (nn*p) - (a-1) div (nn*p));
      if nn <= (2*M) div q then res := res - (b div (nn*q) - (a-1) div (nn*q));
      if nn <= (2*M) div (p*q) then res := res + (b div (nn*p*q) - (a-1) div (nn*p*q));
    end;

  writeln (res);
end.
