【ZCMU1796】wjw的数学题(唯一分解定理+排列组合)

题目链接

1796: wjw的数学题

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 70  Solved: 25
[Submit][Status][Web Board]

Description

Wjw recently encountered a mathematical problem .. given two positive integers x and y, how many kinds of schemes (a, b, c) make their gcd = x, lcm = y

Input

First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive  integers, x and y. The all input is guaranteed to be within the "int"

Output

For each test case, print one line with the number of solutions satisfying the conditions above.

Sample Input

2 6 72 7 33

Sample Output

72 0

HINT

Source

jnxxhzz

【题意】

求满足最大公因数gcd(a,b,c)=x,最小公倍数lcm(a,b,c)=y的(a,b,c)的方案数。

【解题思路】

根据唯一分解定理,a,b,c可以被分解成质因数的乘积形式。

a=x1^a1*x2^a2*x3^a3……

b=x1^b1*x2^b2*x3^b3……

c=x1^c1*x2^c2*x3^c3……

那么gcd(a,b,c)=min(a1,b1,c1)*min(a2,b2,c2)……      lcm(a,b,c)=max(a1,b1,c1)*max(a2,b2,c2)

所以3个数中必须保证每个数都有gcd,并且至少有一个数满足max(a,b,c),所以其余的部分就是由lcm/gcd里面的质因子构成。所以Sample1中的72/6=12(12分解后为2 2 3),因为不同因子之间是不会相互干扰的(这点显而易见吧...?),但是相同因子之间就要考虑干扰问题,比如3个位置都放有2的因子(先不考虑个数),那最终的gcd肯定就不是6了。并且为了满足lcm,必须有一个位置是2的全部因子,有一个位置不能放2的因子,所以对于2这个因子要选择两个位置放置有3种情况(ab,bc,ac),然后这两个位置的一个位置可以选择放全部因子或者不放因子,那么就是2*3=6种情况,另外一个位置可以选择放0-cnt个因子,有cnt种可能(为什么呢?n 0 n和n 0 0)。最后乘起来就是cnt*6啦。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=50005;
int vis[maxn],cnt=0,prime[maxn];
void isprime()
{
    memset(vis,0,sizeof(vis));
	for(int i=2;i<maxn;i++)
	{
		if(!vis[i])prime[cnt++]=i;
		for(int j=0;j<cnt && i*prime[j]<maxn;j++)
		{
			vis[i*prime[j]]=1;
			if(i%prime[j]==0)break;
		}
	}
}
int main()
{
    int T;
    scanf("%d",&T);
    isprime();
    while(T--)
    {
        int x,y,ans=1;
        scanf("%d%d",&x,&y);
        if(y%x!=0)
        {
            printf("0\n");
            continue;
        }
        y/=x;
        for(int i=0;prime[i]<=y;i++)
        {
            if(y%prime[i]==0)
            {
                int cnt=0;
                while(y%prime[i]==0)
                {
                    y/=prime[i];
                    cnt++;
                }
                ans*=cnt*6;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/83349500