Hdu 4497 GCD and LCM(数论)

本文提供了一道来自杭州电子科技大学在线评测系统的数学算法题的解题思路及代码实现。该题需要判断是否存在三个整数x、y、z,使得它们的最大公约数为G,并且满足x + y + z = L。通过分析得出,当L不能被G整除时,一定无解;若有解,则通过对L/G进行质因数分解,计算所有可能的解的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4497

思路:x%G==0,y%G==0,z%G==0,所以L%G==0,若L%G!=0则一定无解。

考虑

L/G=(p1^t1)*(p2^t2)*......*(pn^tn)

x'=x/G=(p1^a1)*(p2^a2)*......*(pn^an)

y'=y/G=(p1^b1)*(p2^b2)*......*(pn^bn)

z'=z/G=(p1^c1)*(p2^c2)*.......*(pn^cn)

对于pi一定有max(ai,bi,ci)==ti,min(ai,bi,ci)==0,否则最大公约数pi^min(ai,bi,ci)*g>g 。

则对于每个pi有

0 0 ti 三种 ti ti ti 三种 0 ti 1--(ti-1) (ti-1)*6种,所以共有6*ti种。

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e6+50;
int v[maxn];
vector<int> prime;
void prepare()
{
    for(int i=2; i<maxn; i++)
    {
        if(!v[i])
        {
            prime.push_back(i);
            for(int j=2*i; j<maxn; j+=i) v[j]=1;
        }
    }
    /*for(int i=0;i<prime.size();i++)
        cout<<i<<" "<<prime[i]<<endl;*/
}
int main()
{
    int t;
    prepare();
    scanf("%d",&t);
    while(t--)
    {
        int L,G,ans=1,tot;
        scanf("%d%d",&G,&L);
        if(L%G)
        {
            printf("0\n");
            continue;
        }
        int tmp=L/G;
        for(int i=0; i<prime.size(); i++)
            if(tmp%prime[i]==0)
            {
                tot=0;
                while(tmp%prime[i]==0)
                {
                    tot++;
                    tmp/=prime[i];
                }
                ans*=6*tot;
            }
        if(tmp>1)ans*=6*tot;
        printf("%d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值