Nastya Studies Informatics
1 second
256 megabytes
standard input
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.
The only line contains four integers l,r,x,y (1≤l≤r≤109, 1≤x≤y≤109).
In the only line print the only integer — the answer for the problem.
1 2 1 2
2
1 12 1 12
4
50 100 3 30
0
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 ;
}
Nastya Studies Informatics的更多相关文章
- Nastya Studies Informatics CodeForces - 992B (大整数)
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- CF992B Nastya Studies Informatics 数学(因子) 暴力求解 第三道
Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input st ...
- Nastya Studies Informatics CodeForces - 992B(增长姿势)
有增长姿势了 如果a * b == lcm * gcd 那么a和b为lcm因数 这个我之前真不知道emm... #include <bits/stdc++.h> #define mem( ...
- 【Codeforces 992B】Nastya Studies Informatics
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 因为gcd(a,b)=x 所以设a = nx b = mx 又有ab/gcd(a,b)=lcm(a,b)=y 则nmx = y 即n(m*x) ...
- CodeForces 992B Nastya Studies Informatics + Hankson的趣味题(gcd、lcm)
http://codeforces.com/problemset/problem/992/B 题意: 给你区间[l,r]和x,y 问你区间中有多少个数对 (a,b) 使得 gcd(a,b)=x lc ...
- Codeforces Round #489 (Div. 2)
A. Nastya and an Array time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- [Codeforces]Codeforces Round #489 (Div. 2)
Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...
- Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices
C. Nastya Is Transposing Matrices time limit per test 1 second memory limit per test 256 megabytes i ...
随机推荐
- Kubernetes里的ConfigMap的用途
顾名思义,ConfigMap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件. ConfigMap同Kubernetes的另一个概念secret类似,区别是ConfigMap主要 ...
- 如何在vue项目中使用sass(scss)
1.用npm/cnpm/yarn安装sass的依赖包 npm install --save-dev sass-loader npm install --save-dev node-sass 或者: y ...
- rpn网络结构再分析
这是rpn网络train阶段的网络结构图 rpn_conv1之前的网络是特征提取层,也是和fast rcnn共享的层.rpn_conv1是一层1*1的卷积,这一层是单独为rpn网络多提取一层特征,这一 ...
- hash 散列表
一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 •比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod •字符串:abc,b ...
- BZOJ1009: [HNOI2008]GT考试 (矩阵快速幂 + DP)
题意:求一个长度为n的数字字符串 (n <= 1e9) 不出现子串s的方案数 题解:用f i,j表示长度为i匹配到在子串j的答案 用kmp的失配函数预处理一下 然后这个转移每一个都是一样的 所以 ...
- AOP日志组件 多次获取post参数
AOP日志组件 多次获取post参数 需求:新增接口日志组件.通过拦截器对接口URL进行拦截处理,然后将接口post请求的参数与结果,写入日志表. 问题:POST方法的参数是存储在request.ge ...
- 【技巧:字符串同构】Avendesora
判断字符串“同构”的技巧 题目大意 给定A,B两个序列,要求B在A中出现的次数以及位置.定义字符变换:把所有相同的字符变为另一种字符:两个字符串相等:当且仅当一个字符串可以在若干次字符变换之后变为另一 ...
- redis:高可用分析
https://www.cnblogs.com/xuning/p/8464625.html 基于内存的Redis应该是目前各种web开发业务中最为常用的key-value数据库了,我们经常在业务中用其 ...
- 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)
NOTE: 1.绝不重新定义一个继承而来的缺省参数值,因为缺省参数值都是静态绑定的,而virtual 函数-----你唯一应该覆盖的东西----却是动态绑定的.
- Mysql 随机函数 rand()
rand() 函数主要有两个用处: 1.是产生随机数, 2.是随机排序(在数据较大的时候会变成性能杀手) 实例: 1.产生一个随机数,默认0~1之间的浮点数 SELECT RAND( ) 2.参数指定 ...