Codeforces992B——Nastya Studies Informatics

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.

先暴力求出y的所有因数
然后再判断这些因数对能不能符合要求

代码:

#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
vector<long long> fac;
set<pair<long long,long long>> s; 
long long l,r,x,y;
long long gcd(long long a,long long b){
    return b==0?a : gcd(b,a%b);
}
int main(void){
    scanf("%lld%lld%lld%lld",&l,&r,&x,&y);
    for(long long i=1;i*i<=y;i++){
        if(y%i==0){
            fac.push_back(i);
            if(i!=y/i){
                fac.push_back(y/i);
            }
        }
    }   
    int len=fac.size();
    for(int i=0;i<len;i++){
        for(int j=0;j<len;j++){
            if(fac[i]>=l && fac[i]<=r && fac[j]>=l && fac[j]<=r && gcd(fac[i],fac[j])==x && x*y==fac[i]*fac[j]){
                s.insert({fac[i],fac[j]});
            }
        }
    }
    printf("%d\n",int(s.size()));
    return 0;
}
全部评论

相关推荐

菠落蜜:这个是系统自动投的,不是hr主动打招呼。更抽象的还有ai回复
我的秋招日记
点赞 评论 收藏
分享
10-10 17:46
门头沟学院 Java
廖依然:所以能分我一个offer吗?求求了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务