#include <bits/stdc++.h>
#define lli long long int
#define ulli unsigned long long int
using namespace std;
ulli l,r,p,q;
vector<ulli> vp,vq;
ulli gcd(ulli x, ulli y)
{
if (y>x) swap(x,y);
while (y)
{
ulli r = x%y;
x = y;
y = r;
}
return x;
}
ulli lcm(ulli x , ulli y)
{
ulli g = gcd(x,y);
ulli X = x / g;
ulli Y = y;
ulli M = (ulli)(1e18)/X;
if ( Y > M ) return -1; // not valid
X *= Y;
return X;
}
ulli f(ulli n)
{
ulli ans = 0;
int vps = vp.size() , vqs = vq.size();
for (int i=vps-1 ; i>0 ; i--)
{
if (vp[i] > n) continue;
ulli that = (i==vps-1 ? 0 : n/vp[i+1]);
ulli thiz = n/vp[i];
ulli cnt = thiz - that;
ans += cnt;
if (i < vqs)
{
if (vq[i]>n) continue;
ulli b = lcm(vp[i] , vq[i]);
if (b==-1) continue;
ans -= n/b;
if (i!=vps-1)
{
ulli b2 = lcm(vp[i+1] , b);
if (b2==-1) continue;
ans += n/b2;
}
}
}
return ans;
}
int main ()
{
cin>>l>>r>>p>>q;
for(ulli i=1,e=(r/p) ; 1 ; i*=p) { vp.push_back(i); if (i>e) break; }
for(ulli i=1,e=(r/q) ; 1 ; i*=q) { vq.push_back(i); if (i>e) break; }
ulli L = f(l-1);
ulli R = f(r);
cout<<(R-L);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGxpIGxvbmcgbG9uZyBpbnQKI2RlZmluZSB1bGxpIHVuc2lnbmVkIGxvbmcgbG9uZyBpbnQKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnVsbGkgbCxyLHAscTsKdmVjdG9yPHVsbGk+IHZwLHZxOwoKdWxsaSBnY2QodWxsaSB4LCB1bGxpIHkpCnsKCWlmICh5PngpIHN3YXAoeCx5KTsKCXdoaWxlICh5KQoJewoJCXVsbGkgciA9IHgleTsKCQl4ID0geTsKCQl5ID0gcjsKCX0KCXJldHVybiB4Owp9Cgp1bGxpIGxjb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