首页 > 其他分享 >B. Nastya Studies Informatics

B. Nastya Studies Informatics

时间:2023-04-05 20:01:17浏览次数:60  
标签:integers pairs good GCD LL Informatics Nastya Studies


B. Nastya Studies Informatics

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

1 2 1 2

Output

Copy

2

Input

Copy

1 12 1 12

Output

Copy

4

Input

Copy

50 100 3 30

Output

Copy

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.

解释  : 

Let's consider some suitable pair

B. Nastya Studies Informatics_#include

. As

B. Nastya Studies Informatics_ide_02

, we can present number

B. Nastya Studies Informatics_ios_03

as

B. Nastya Studies Informatics_ide_04

, and number

B. Nastya Studies Informatics_ide_05

as

B. Nastya Studies Informatics_ide_06

, where we know that

B. Nastya Studies Informatics_ios_07

and

B. Nastya Studies Informatics_#include_08

.Let's consider too that from the restriction from the problem

B. Nastya Studies Informatics_ios_09

we surely know the restriction for

B. Nastya Studies Informatics_ios_10

and

B. Nastya Studies Informatics_ios_11

, that is

B. Nastya Studies Informatics_ios_12

.Let's remember we know that

B. Nastya Studies Informatics_#include_13

(because

B. Nastya Studies Informatics_#include_14

is

B. Nastya Studies Informatics_#include_15

,

B. Nastya Studies Informatics_ide_16

is

B. Nastya Studies Informatics_ios_17

). Then we can get

B. Nastya Studies Informatics_#include_18

. Dividing by

B. Nastya Studies Informatics_#include_14

:

B. Nastya Studies Informatics_ide_20

.

B. Nastya Studies Informatics_#include_21

.Now if

B. Nastya Studies Informatics_#include_22

, answer equals 0.Else as

B. Nastya Studies Informatics_ide_23

is surely less than

B. Nastya Studies Informatics_#include_24

, we can just sort out all possible pairs

B. Nastya Studies Informatics_ide_25

of divisors

B. Nastya Studies Informatics_ide_23

, such that

B. Nastya Studies Informatics_#include_27

, and then to check that

B. Nastya Studies Informatics_#include_08

and

B. Nastya Studies Informatics_ios_29

are in the getting above restrictions. Complexity of this solution is

B. Nastya Studies Informatics_ide_30

.

题意:a,b为两个正整数,l<= a,b <=r。x=gcd(a,b),y=lcm(a,b)。gcd是a,b的最大公约数,lcm是a,b的最小公倍数。给出l,r,x,y,求有多少种可能的a,b。

gcd(a,b)= x ,lcm(a,b) = y ;

那么有x*y = a*b ; 

设a = x*c;

  b = x*d ;  并且 gcd(c,d) ==   1 ;

那么 a*b = x*y = x*c*x*d ;   

即 

B. Nastya Studies Informatics_ide_31

= c*d ;  然后枚举 d ,满足gcd(c,d) = 1  , 还有

B. Nastya Studies Informatics_ios_12

 . 

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std ;
typedef long long LL ;
LL GCD(LL a ,LL b )
{

    return b?GCD(b,a%b):a ;

}
LL LCM(LL a ,LL b)
{
    return a*(b/GCD(a,b));

}
int main()
{
   int l, r, x, y;
    cin >> l >> r >> x >> y;
    if (y % x != 0){
        cout << 0 << '\n';
        return 0;
    }
    int ans = 0;
    int n = y / x;
    for (int d = 1; d * d <= n; ++d) {
        if (n % d == 0) {
            int c = n / d;
            if (l <= c * x && c * x <= r && l <= d * x && d * x <= r && GCD(c, d) == 1) 
            {
                if (d * d == n) 
                ++ans;
                else ans += 2;
            }
        }
    }

    cout << ans << '\n';


    return 0 ;
}

 

 

 

 

 

 

 

 

 

 

标签:integers,pairs,good,GCD,LL,Informatics,Nastya,Studies
From: https://blog.51cto.com/u_15970235/6171519

相关文章