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
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≤rl≤a,b≤r.


题意就是给你一个范围(l,r),让你找有多少个数对(a,b)满足a和b的最大公约数是x,最小公倍数是y

思路就是对x,y进行质因数分解,这样对于每一个质因数,就会有一个上界和一个下界,而a,b对应的质因数取值也只能取上界或者下界

这样就可以把所有满足条件的数对都找出来,然后再一个个的判断是否在(l,r)范围里。

很显然数对的个数不会超过2的二十次方左右,所以不会超时。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<functional>
#include<queue>
#include<vector>
#include<algorithm>
#define N 1000005
using namespace std; long long prime[N+],len=,pd[N+]= {}; void shushu()
{
pd[]=pd[]=; for(int i=; i<=N; i++)
{
if(pd[i]==)
{
prime[len++]=i;
} for(int j=; j<len; j++)
{
if(i*prime[j]>=N)
break;
pd[i*prime[j]]=;
if(i%prime[j]==)
break;
}
}
} long long l,r,x,y,team[],team1[]= {},team2[]= {},c1=,ans=; long long ff(long long a,long long b);
void f(long long x,long long a,long long b)
{ if(x==c1)
{
if(a>=l&&a<=r&&b>=l&&b<=r)
{
ans++; } return;
}
f(x+,a,b); if(team1[x]!=team2[x])
f(x+,a/ff(team[x],team1[x])*ff(team[x],team2[x]),b/ff(team[x],team2[x])*ff(team[x],team1[x])); } long long ff(long long a,long long b)
{
long long ans=; while(b>)
{
if(b%==)
{
ans*=a; } a*=a;
b/=; }
return ans;
} int main()
{
shushu(); scanf("%I64d %I64d %I64d %I64d",&l,&r,&x,&y); if(y%x!=)
{
printf("");
return ;
} for(int i=; i<len; i++)
{
if(y%prime[i]==)
team[c1++]=prime[i]; while(y%prime[i]==)
{
team2[c1-]++;
y/=prime[i]; } } if(y>)
{
team[c1++]=y;
team2[c1-]++;
} for(int i=; i<c1; i++)
{
while(x%team[i]==)
{
team1[i]++;
x/=team[i];
}
} long long a=,b=; for(int i=; i<c1; i++)
a*=ff(team[i],team1[i]);
for(int i=; i<c1; i++)
b*=ff(team[i],team2[i]); f(,a,b);
printf("%I64d",ans);
return ;
}

最新文章

  1. (八)map,filter,flatMap算子-Java&amp;Python版Spark
  2. redis 相关命令
  3. Linux系统入门学习:在curl中设置自定义的HTTP头
  4. SOAP 格式设置选项
  5. AppExtention - today
  6. 谈谈一些有趣的CSS题目(十五)-- 谈谈 CSS 关键字 initial、inherit 和 unset
  7. BootStrap基礎知識
  8. bzoj:1575: [Usaco2009 Jan]气象牛Baric
  9. [Ahoi2005]LANE 航线规划
  10. Spinner之下拉多选,监听ID后显示不同Frgment页面
  11. adb常用命令(golang版)及输入中文
  12. h5完美实现无刷新上传并附带上传效果
  13. Spring 12 种 常用注解!
  14. hdu2295DLX重复覆盖+二分
  15. Python: re.sub()第二个参数
  16. SQL新增数据取表主键最新值
  17. 虚拟机vmware centos7 扩展磁盘空间
  18. Java Callable接口与Future接口的两种使用方式
  19. centos7.2+php7.2+nginx1.12.0+mysql5.7配置
  20. 第五周作业总结(内含用Junit测试ArrayStack和LinkedStack课堂练习报告)

热门文章

  1. MATLAB批量修改图片名称
  2. The Django Book - 第四章 模板2
  3. xcode或者mac自带颜色器选择rgb格式
  4. Unity3d中MonoBehavior默认函数的执行顺序和生命周期
  5. block总结
  6. UEditor练习(JSP版)
  7. Mybatis generator自动生成代码包括实体,dao,xml文件
  8. Js图片缩放代码 鼠标滚轮放大缩小 图片向右旋转
  9. iMessage, Facetime 解决办法
  10. x220 OS X 10.10.4安装