B. Nastya Studies Informatics(数学)

该问题要求找到在给定范围内(l到r)的整数对(a, b),使得它们的最大公约数GCD(a, b)等于x,且最小公倍数LCM(a, b)等于y。通过解析题目条件,可以得出解题思路:由于x*y = a*b,可以枚举a的值,然后计算对应的b值,确保它们在给定范围内。最终计算满足条件的整数对数量。" 107446756,9058892,高可用架构:负载均衡策略与实现,"['分布式', 'nginx', '数据库', 'java', 'linux']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)(a,b) good, if GCD(a,b)=xGCD(a,b)=x and LCM(a,b)=yLCM(a,b)=y, where GCD(a,b)GCD(a,b) denotes the greatest common divisor of aa and bb, and LCM(a,b)LCM(a,b) denotes the least common multiple of aa and bb.

You are given two integers xx and yy. You are to find the number of good pairs of integers (a,b)(a,b) such that la,brl≤a,b≤r. Note that pairs (a,b)(a,b) and (b,a)(b,a) are considered different if aba≠b.

Input

The only line contains four integers l,r,x,yl,r,x,y (1lr1091≤l≤r≤1091xy1091≤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)(a,b)(1,2)(1,2) and (2,1)(2,1).

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

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

题意: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。

思路:如果直接暴力,利用x*y=a*b 来做的话,肯定会T!现在,我们设 a=n*m,b=m*z.那么,x=m,y=n*m*z,a*b=n*m*m*z。如果我们来枚举a的所有可能的话,1e9,太大,所以我们考虑枚举n的所有可能!!我们先来确定n的范围,最小为1,n最大值为sqrt(n*z),确定了范围后,就可以开始写了。

   #include "iostream"
    using namespace std;
    int gcd(int a,int b)
    {
        if(b==0) return a;
        return gcd(b,a%b);
    }
    int main()
    {
        int l,r,x,y,i,num,ans=0;
        cin>>l>>r>>x>>y;
        num=y/x; if(y%x!=0){cout<<"0"<<endl;return 0;}//num=n*z
        for(i=1;i*i<=num;i++){
            int j=num/i;
            if(num%i==0&&gcd(i,j)==1&&l<=i*x&&i*x<=r&&l<=j*x&&j*x<=r) ans+=i==j?1:2;
        }
        cout<<ans<<endl;
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值