LightOJ-1431 Aladdin and the Flying Carpet (算术基本定理 + 求约数个数)

LightOJ-1431  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}.

Input

Input starts with an integer T (≤ 4000), denoting the number of test cases.

Each case starts with a line containing two integers: a b (1 ≤ b ≤ a ≤ 1012) where a denotes the area of the carpet and bdenotes the minimum possible side of the carpet.

Output

For each case, print the case number and the number of possible carpets.

Sample Input

2

10 2

12 2

Sample Output

Case 1: 1

Case 2: 2

约数个数/2- 到小于b的所有约数就是答案

b是乘号的左边 左边小于右边,因为不能是正方形,所以 b<sqrt(n);

 

时间复杂度 (logn+1)*根号n

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 1000047
long long p[MAXN], prime[MAXN];  //这个prime可以存1~MAXN内的素数
int k=0;
void getprime()
{
    long long i,j;
    memset(p,0,sizeof(p));
    memset(prime,0,sizeof(prime));
    for(i=2;i<MAXN;i++)
    {
        if(p[i]==0)
        {
            prime[k++]=i;
            for(j=2*i;j<MAXN;j+=i)
            {
                p[j]=1;
            }
        }
    }
    prime[0]=2;
}                                  
long long factor_cnt(long long n)  //这个算约数个数函数只要算√n内的素数就行了
{
    long long sum=1;
    if(n==0)
    return 0;
    long long a=0;
    long long i=0;
    while(prime[i]*prime[i]<=n&&i<k)
    {
        a=0;
        if(n%prime[i]==0)
        {
            while(n%prime[i]==0)
            {
                n/=prime[i];
                a++;
            }
        }
        sum*=1+a;
        i++;
    }
    if(n > 1)
    sum*=1+1;
    return sum;
}
int main()
{
    long long a, b;
    int t;
    int cas=0;
    getprime();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        int cnt=0;
        long long num,ans;
        if(b>=sqrt(a))
        ans=0;
        else
        {
            for(long long i=1;i<b; i++)
            if(a%i==0)
            cnt++;
            num=factor_cnt(a)/2;
            ans=num-cnt;
        }
        printf("Case %d: %lld\n",++cas,ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值