Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
-
p and q are positive integers;
-
GCD(p, q) = x;
-
LCM(p, q) = y.
Input
x and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
给你 x,y。gcd和lcm 问这样的对数有多少。
转载:http://blog.youkuaiyun.com/xieshimao/article/details/6439803
先说说解法吧。
假设g=gcd(p,q),l=lcm(p,q),key=l/g
我们可以想象一下:
那个key是什么?想清楚这个问题就很简单了。
l是p和q公共的素因子的并集,而g则是交集(仔细理解这句话!!)
于是key的意思就是l刚好比g多的一部分素因子
于是我们可以选择这些素因子和g组合得到了p
另一部分和g组合得到了q
假设key的素因子个数(重复的不算)有n个
那么选择方案就有:C(n,0)+C(n,1)+C(n,2)+…+C(n,n)
熟悉组合学公式的童鞋肯定很快就可以反应过来
上面那个式子的和为2^n,所以剩下的工作就非常的简单了
注意点1:要求y/x的 因式分解,这样才是多余那部分。
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
vector<int> solve(int x)
{
vector<int> ans;
for(int i = 2; i * i <= x; i++)
{
if(x%i == 0)
{
ans.push_back(i);
while(x%i == 0)
x /= i;
}
}
if(x!=1) ans.push_back(x);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int x,y;
while(cin >> x >>y)
{
vector<int> v1 = solve(y/x);
if(y % x != 0)
{
printf("0\n");
continue;
}
int n =v1.size();
printf("%lld\n",(ll)pow(2ll,n) );
}
return 0;
}