lightoj 1341__Aladdin and the Flying Carpet

题目描述:

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery.
Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised himself as Aladdin's uncle, found a strange magical flying carpet at the entrance. There were some strange creatures guarding the entrance of the cave. Aladdin could run, but he knew that there was a high chance of getting caught. So, he decided to use the magical flying carpet. The carpet was rectangular shaped, but not square shaped. Aladdin took the carpet and with the help of it he passed the entrance.
Now you are given the area of the carpet and the length of the minimum possible side of the carpet, your task is to find how many types of carpets are possible. For example, the area of the carpet 12, and the minimum possible side of the carpet is 2, then there can be two types of carpets and their sides are: {2, 6} and {3, 4}.

题意就是给你一个a,b然后求[b,a]区间内a的约数对数。

输入输出直接看样例吧:
input:

2

10 2

12 2

output:

Case 1: 1

Case 2: 2

一道应用算术基本定理的数论题,至于算数基本定理百度百科,

我们利用这个定理可以计算出a的约数个数,由于约数成对,直接除以2就是约数对数,然后枚举区间[1,b]内a的约数,每有一个,则答案的约数对数-1,最后得出结果;
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXM=1000010;

bool prime[MAXM];
int rec[MAXM], cnt;
void init_prime_table()
{
    memset(prime,true,sizeof(prime));
    prime[0]=prime[1]=false;
    for (int i = 2; i<=MAXM; ++i)
    {
        if (prime[i])
            rec[cnt++] = i;
        for (int j =0;j<cnt&&rec[j]<=MAXM/i;++j)
        {
            prime[i*rec[j]]=false;
            if (i%rec[j]==0)
                break;
        }
    }
}

int cal(long long x)
{
    int num=1;
    for(int i=0;i<cnt&&rec[i]<=sqrt(x);i++)
    {
        int cc=0;
        while(x%rec[i] == 0)
        {
            cc++;
            x/=rec[i];
        }
        num*=(cc+1);
    }
    if(x>1)
        num*=2;
    return num;
}
int main()
{
    int T;
    scanf("%d",&T);
    int cas=1;
    cnt=0;
    init_prime_table();
    while(T--)
    {
        long long a,b;
        scanf("%lld%lld",&a,&b);
        if (a<b*b)
        {
            printf("Case %d: 0\n",cas);
            cas++;
            continue;
        }
        else
        {
            int num=cal(a);
            num=num/2;
            for (int i=1;i<b;i++)
                if (a%i==0)
                num--;
            printf("Case %d: %d\n",cas,num);
            cas++;
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值