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

78 篇文章 0 订阅


Aladdin and the Flying Carpet
Time Limit: 3 second(s)Memory Limit: 32 MB

It's said that Aladdin had to solve seven mysteries beforegetting the Magical Lamp which summons a powerful Genie. Here we are concernedabout the first mystery.

Aladdin was about to enter to a magical cave, led by theevil sorcerer who disguised himself as Aladdin's uncle, found a strange magicalflying carpet at the entrance. There were some strange creatures guarding theentrance of the cave. Aladdin could run, but he knew that there was a highchance of getting caught. So, he decided to use the magical flying carpet. Thecarpet was rectangular shaped, but not square shaped. Aladdin took the carpetand with the help of it he passed the entrance.

Now you are given the area of the carpet and the length ofthe minimum possible side of the carpet, your task is to find how many types ofcarpets are possible. For example, the area of the carpet 12, and the minimumpossible side of the carpet is 2, then there can be two types of carpets andtheir 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: ab (1 ≤ b ≤ a ≤ 1012) where adenotes the area of the carpet and b denotes the minimum possible sideof the carpet.

Output

For each case, print the case number and the number ofpossible carpets.

Sample Input

Output for Sample Input

2

10 2

12 2

Case 1: 1

Case 2: 2

 

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341


题目大意:给两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组对数,(c,d)和(d,c)属于同一种情况


题目分析:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个数为num = (1 + a1) * (1 + a2) *...(1 + ai),这里的ai是素因子的指数,见唯一分解定理,因为题目说了不会存在c==d的情况,因此num要除2,去掉重复情况,然后枚举小于b的a的约数,拿num减掉就可以了


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
int const MAX = 1e6 + 10;
int p[MAX];
bool u[MAX];
int num, cnt;
ll a, b, tmp;

void get_prime()  
{  
    memset(u, false, sizeof(u));
    for(int i = 2; i <= sqrt(MAX); i++)
        if(!u[i])
            for(int j = i * i; j <= MAX; j += i)
                u[j] = true;
    for(int i = 2; i <= MAX; i++)
        if(!u[i])
            p[cnt ++] = i;
}

void cal()
{
    for(int i = 0; i < cnt && p[i] <= sqrt(tmp); i++)
    {
        int cc = 0;
        while(tmp % p[i] == 0)
        {
            cc ++;
            tmp /= p[i];
        }
        num *= (cc + 1);

    }
    if(tmp > 1) //如果tmp不能被整分,说明还有一个素数是它的约数,此时cc=1
        num *= 2;
}

int main()
{
    int T;
    scanf("%d", &T);
    cnt = 0;
    get_prime();
    for(int ca = 1; ca <= T; ca++)
    {
        scanf("%lld %lld", &a, &b);
        if(a < b * b)
            printf("Case %d: 0\n", ca);
        else
        {
            num = 1;
            tmp = a;
            cal();
            num /= 2;
            for(int i = 1; i < b; i++)
                if(a % i == 0)
                    num --;
            printf("Case %d: %d\n", ca, num);
        }
    }
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值