lightoj 1341 - Aladdin and the Flying Carpet / lightoj 1236 - Pairs Forming LCM(算术基本定理)

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

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 b denotes the minimum possible side of the carpet.

Output

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

Sample Input

Output for Sample Input

2

10 2

12 2

Case 1: 1

Case 2: 2



这题的题意是让你求a的因子中大于b的个数,可以用算术基本定理求出n所有因子的个数,然后再减去小于b的因子的个数,就可以得到答案(虽然我感觉b也挺大的,如果每次都找比b小的因子的个数,也有可能超时,但是没办法网上都是这么写的,Orz~)

那么, 什么是算术基本定理呢?

任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积 N=P1a1P2a2P3a3......Pnan,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数。

n的正因子的个数就等于 (1+a1)*(1+a2)*...*(1+an)。

其实对应的每一个质因数的不同幂次,就能组成一个n的因子。(理解这个就相当于理解了这个定理)

这题就直接套用定理就行了。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

const int maxn = 1000010;
int cnt = 0;
int flag[maxn],p[maxn];
void prime(void)
{
    LL i,j;
    flag[1]=1;
    for(i=2;i<maxn;i++)
    {
        if(flag[i])
            continue;
        p[cnt++] = i;
        for(j=i*i;j<maxn;j+=i)
        {
            flag[j] = 1;
        }
    }
}

int main(void)
{
    int T,i,j;
    prime();
    LL a,b;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%lld%lld",&a,&b);
        printf("Case %d: ",cas++);
        if(b >= sqrt(a))
        {
            printf("0\n");
            continue;
        }
        LL ta = a;
        int ans = 1;
        i = 0;
        while(p[i]*p[i] <= a && i < cnt)
        {
            int t = 0;
            while(a % p[i] == 0)
            {
                t++;
                a /= p[i];
            }
            ans *= t+1;
            i++;
        }
        if(a > 1)
            ans *= 2;
        ans /= 2;
        for(i=1;i<b;i++)
            if(ta % i == 0)
                ans--;
        printf("%d\n",ans);
    }

    return 0;
}

1236 - Pairs Forming LCM

Find the result of the following code:

long long pairsFormLCM( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
        for( int j = i; j <= n; j++ )
           if( lcm(i, j) == n ) res++; // lcm means least common multiple
    return res;
}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

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

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

Output

For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

Sample Input

Output for Sample Input

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

Case 1: 2

Case 2: 2

Case 3: 3

Case 4: 5

Case 5: 4

Case 6: 5

Case 7: 8

Case 8: 5

Case 9: 8

Case 10: 8

Case 11: 5

Case 12: 11

Case 13: 3

Case 14: 4

Case 15: 2



这题的题意是就是让你找有多少对lcm(a,b) = n,(a,b)和(b,a)算同一组。

(这段是摘自一个聚聚的blog:http://blog.csdn.net/qq_15714857/article/details/48641121)

这就是我上面说的每一种质因数幂次的组合,都是一个因子。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

const int maxn = 10000010;
int cnt = 0;
bool flag[maxn];
int p[maxn/10];

void prime(void)
{
    LL i,j;
    flag[1]=1;
    for(i=2;i<maxn;i++)
    {
        if(flag[i])
            continue;
        p[cnt++] = i;
        for(j=i+i;j<maxn;j+=i)
        {
            flag[j] = 1;
        }
    }
}

int main(void)
{
    int T,i,j;
    LL n;
    prime();
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%lld",&n);
        i = 0;
        LL ans = 1;
        while(p[i]*p[i] <= n && i < cnt)
        {
            int t = 0;
            while(n % p[i] == 0)
            {
                n /= p[i];
                t++;
            }
            ans *= 2*t+1;
            i++;
        }
        if(n > 1)
            ans *= 3;
        ans = (ans+1)/2;
        printf("Case %d: %lld\n",cas++,ans);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值