hdu 4497

GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1941 Accepted Submission(s): 860

Problem Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.

Input

First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.

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
分析:这题明显要用唯一分解定理来做。gcd(x,y,x)=G,lcm(x,y,z)=L;
x=p1^a1*p2^a2……ps^as;
y=p1^b1*p2^b2……ps^bs;
z=p1^c2*p2^c2……ps^cs;
G,L已知,满足条件:
G=p1^min(a1,b1,c1)……ps^min(as,bs,cs)=p1^e1……ps^es
L=p1^max(a1,b1,c1)……ps^max(as,bs,cs)=p1^h1……ps^hs
则满足 ei=min(ai,bi,ci) hi=max(ai,bi,bi),考虑组合数。
考虑有序数对,对于每个(ei,hi) ,让 (x,y,z)固定两个数,让一个数变化有 6种,即 A(3,2)=6,每一种有(hi-ei+1)种。
然后考虑重复情况,每次,只要(x,y,z)中只出现ei,hi的情况重复,总共重复6次。即 6*(hi-ei+1)-6=6*(hi-ei)种。
即 6*(h1-e1)*6(h2-e2)……,由于是幂指数相减,考虑L/G就可。如果不能整除,即没有(x,y,z)满足条件。公式推出,题目就好做了!

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e7+5;
bool vis[maxn];
int  prime[maxn/10],factor[maxn],len=0;

void is_prime()//素数打表
{
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])
        {
            prime[len++]=i;
            for(int j=i+i;j<maxn;j+=i)
            {
                vis[j]=1;
            }
        }
    }
}

void getprimefactor(int nn)//得到素数幂指数
{
    int cas=0;
    for(int i=0;i<len&&prime[i]*prime[i]<=nn;i++)
    {
            while(nn%prime[i]==0)
            {
                factor[cas]++;
                nn/=prime[i];
            } 
          if(factor[cas])
          cas++;
    }
    if(nn>1)
    factor[cas]=1;
}

int main()
{
    is_prime(); 
    int t,g,l;
     scanf("%d",&t);
     while(t--)
     {
        memset(factor,0,sizeof(factor));
        int sum=1;
        scanf("%d %d",&g,&l);
        if(l%g)
        {
            printf("0\n");
            continue;
         }
         int k=l/g;
         getprimefactor(k);

         for(int i=0;;i++)
         {
            if(!factor[i])
            break;
            sum*=factor[i];
            sum*=6;
         }
         printf("%d\n",sum);
      } 
      return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值