1. 程式人生 > >Nastya Studies Informatics【好多細節、好坑的題,或許是我太弱了吧,不過我找到了WA的原因了】

Nastya Studies Informatics【好多細節、好坑的題,或許是我太弱了吧,不過我找到了WA的原因了】

Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.

We define a pair of integers (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the 

greatest common divisor of a and b, and LCM(a, b) denotes the least common multiple of a and b.

You are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l ≤ a, b ≤ r. Note that pairs (a, b) and (b, a) are considered different if a ≠ b.

Input

The only line contains four integers l

, r, x, y (1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ y ≤ 109).

Output

In the only line print the only integer — the answer for the problem.

Examples

Input

1 2 1 2

Output

2

Input

1 12 1 12

Output

4

Input

50 100 3 30

Output

0

Note

In the first example there are two suitable good pairs of integers (a, b

): (1, 2) and (2, 1).

In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).

In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l ≤ a, b ≤ r.

這道題感觸挺深的。。。

不用數了,一共WA了20次

思路

  題目要的是在(l, r)區間內,找一對(a, b)滿足它們的gcd==x與lcm==y,那麼我們該如何去尋這樣的(a, b)?首先,令a==qx, b==px,則有gcd(qx, px)==x,即gcd(q, p)==1,故q、p互質,又知道lcm(qx, px)==y,於是有lcm(q, p)==(y/x),又因為q、p互質,所以q*p==y/x。所以,我們只需要在(1~sqrt(y/x))的區間內尋找這樣一對滿足條件的式子了。

細節

  1. 題目沒有說y一定能被x整除,所以要特判這種狀況。

  2. 當兩個數相等的時候,就是隻有一種方法了。

  3. 不要用l/x, r/x這樣的方法簡化,因為它們是向下取整的,可能會漏值,還是老實的用i*x>=li*x<=r這樣的方式比較穩妥(WA on 39的原因)

完整程式碼

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
ll l, r, x, y;     //尋這樣的(a, b),發現有a*b==x*y,我令a=qx,b=px,gcd(qx, px)=x,故gcd(q,  p)==1
ll gcd(ll a, ll b)       //我們只需要在(l/x, r/x)區間內尋找是否有一對y/x的公因子即可
{                           //gcd(p, q)==1滿足該條件才行
    if(b==0) return a;      //則又有p、q互質,所以y/x==p*q此時滿足條件的一對便是
    return gcd(b, a%b);
}
int main()
{
    while(scanf("%lld%lld%lld%lld", &l, &r, &x, &y)!=EOF)
    {
        if(y%x) { printf("0\n"); continue; }
        ll x_y=y/x;
        int ans=0;
        for(ll i=1; i*i<=x_y; i++)
        {
            if(i*x>=l && x_y%i==0 && x_y/i*x<=r && gcd(i, x_y/i)==1) ans+=(i==x_y/i?1:2);
        }
        printf("%d\n", ans);
    }
    return 0;
}