lightoj 1341__Aladdin and the Flying Carpet

来源:互联网 发布:java员工工资管理系统 编辑:程序博客网 时间:2024/06/03 20:41

题目描述:

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;}



原创粉丝点击