zcmu 1796 wjw的数学题 (唯一分解定理+排列组合)

【题目】

Problem B: wjw的数学题

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 69  Solved: 24
[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

【题意】

给定三个数的最大公约数x与最小公倍数y,问存在多少对符合要求的(a,b,c)。

【思路】

简单的思考一下,abc一定属于[x,y]并且是x的倍数,由此得若y%x!=0,则不存在,输出0。

根据唯一分解定理每个a,b,c都可以分成素数幂形式;

a=p1^a1*p2^a2*…pn^an;

b=p1^b1*p2^b2*…pn^bn;

c=p1^c1*p2^c2*…pn^cn;

gcd(a,b,c)=p1^min(a1,b1,c1)*…pn^min(an,bn,cn);

lcm(a,b,c)=p1^max(a1,b1,c1)*…pn^max(an,bn,cn);

显然对于每个质因数,只要三个数中有一个数的质因数的个数满足等于max(a,b,c)就可以了。这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由lcm/gcd里面的质因子构成。这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现干扰,因为不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子。对于单种因子来说,每种因子选择两个位置放置有 3 种情况,而两个位置里边选择一个位置放全部即n个因子有 2 种情况, 就有 3 *  2 =6 种情况,而另一个位置可以放0-n个因子有n种情况,这样化简后就是6 * n。由于不同因子互不干扰,所以最后的答案是各质因数的放置情况的乘积。

                                    对不起我没有脑子...

【代码】

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define Pi acos(-1)
#define eps 1e-8
using namespace std;
typedef long long int ll;
const int maxn=1e6;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
bool number[maxn+5];
int prime[maxn+5];
void isprime()
{
    int i,j,c=0;
    memset(number,true,sizeof(number));
    for(i=2;i<=maxn;i++)
    {
        if(number[i])
            prime[c++]=i;
        for(j=0;j<c&&prime[j]*i<=maxn;j++)
        {
            number[prime[j]*i]=false;
            if(i%prime[j]==0)
                break;
        }
    }
}
main()
{
    isprime();
    int t; scanf("%d",&t);
    while(t--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(y%x)
        {
            printf("0\n");
            continue;
        }
        int c=1,n=y/x,i=0;
        while(prime[i]<=n)
        {
            if(n%prime[i]==0)
            {
                int sum=0;
                while(n%prime[i]==0)
                {
                    n/=prime[i];
                    sum++;
                }
                c*=6*sum;
            }
            i++;
        }
        printf("%d\n",c);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值