首页 > 其他 > 详细

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

时间:2015-07-27 21:06:47      阅读:1029      评论:0      收藏: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);
        }
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

原文:http://blog.csdn.net/tc_to_top/article/details/47089183

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!