LightOJ - 1341 Aladdin and the Flying Carpet(唯一分解定理)

#include<cstdio>
using namespace std;
typedef long long LL;
/*
唯一分解定理的运用
一个数可以唯一分解为n=p1^s1 * p2^s2 * …… pk*sk
n的正因数的个数可以表示为: num = (s1+1)*(s2+1)…(sk+1);(+1是因为可以不选这个质因数)

题中是求a=m*b (m,b)的对数且要求m>=b,因此将num/2避免重复,其中小于b的约数也是不合法的

*/
const int maxn=1e6+5;
int n;
LL a,b;
int prime[maxn];//素数
bool is_prime[maxn];//i是否是素数
int p;//素数个数
//素数筛
void sieve(int n)
{
    p=0;
    for(int i=0; i<n; i++) is_prime[i]=1;
    is_prime[0]=is_prime[1]=0;
    for(int i=2; i<n; i++)
    {
        if(is_prime[i])
        {
            prime[p++]=i;
            for(int j=2*i; j<n; j+=i)
            {
                is_prime[j]=0;
            }
        }
    }
}

int solve(LL a)
{

    int cnt;
    int ans=1;
    LL t=a;
    if(a/b<b) return 0;//不存在合法约数
    //运用唯一分解定理进行分解
    for(int i=0; i<p&&prime[i]*prime[i]<=t; i++)
    {
        if(t%prime[i]==0)
        {
            cnt=0;//计算质因数i的指数
            while(t%prime[i]==0)
            {
                t/=prime[i];
                cnt++;
            }
            ans=ans*(cnt+1);//a约数总的个数
        }
    }
    if(t>1)//如果a是质数
    {
        ans=ans*(1+1);
    }
    ans=ans>>1;//避免重复
    for(LL i=1; i<b; i++) //去除小于b的约数
    {
        if(a%i==0)
        {
            ans--;
        }
    }
    return ans;
}

int main()
{
    sieve(maxn);
    int T;
    scanf("%d",&T);
    int kase=0;
    while(T--)
    {
        scanf("%lld%lld",&a,&b);
        printf("Case %d: %d\n",++kase,solve(a));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值