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

 题意:求a的所有因数对中大于等于b的因数对。即给两个数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减掉就可以了

唯一分解定理:任何>1的整数都可以分解成有限个质数的乘积即n = P1^a1 * P2^a2 * …………*Pn^an (P1 < P2 < ……Pn);其中P为质因数,a为质因数的指数。 

即:每个大于1的自然数均可写为质数的积,而且这些素因子按大小排列之后,写法仅有一种方式。(唯一)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
const long long int N=1e6;
typedef long long ll;
using namespace std;
ll prime[N],a,b,p[N],k,num;
void sss()
{
    k=0;
    for(ll i=2; i<=N; i++)
    {
        if(p[i]==0)
        {
            prime[k++]=i;///素数
            for(ll j=i*2; j<=N; j+=i)
                p[j]=1;///合数
        }
    }
}
ll w(ll a)
{
    num=1;
    for(int i=0; i<k&&prime[i]<a; i++)
    {
        int x=0;
        while(a%prime[i]==0)
        {
            a/=prime[i];
            x++;
        }
        num*=x+1;
    }
    if(a>1)num*=1+1;///多一个素数
    return num;
}
int main()
{
    sss();///素数筛-->TLE
    int t,flag=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld",&a,&b);
        {
            if(a<b*b)
                printf("Case %d: 0\n",flag++);
            else
            {
                ll ans=w(a)/2;///a的素因子   half
                for(int i=1; i<b; i++)
                    if(a%i==0)
                       ans--;
                printf("Case %d: %lld\n",flag++,ans);
            }
        }
    }
    return 0;
}
/*
算术基本定理:

分解素因数:n=(p1^k1)*(p2^k2)*...*(pn*kn).(分解方式唯一)

n的约数个数为ans(n)=(1+k1)*(1+k2)*...*(1+kn).

根据唯一分解定理先将a唯一分解,则a的所有正约数的个数为
ans = (1 + a1) * (1 + a2) *...(1 + ai)

因为题目说了不会存在c==d的情况,因此cnt要除2,去掉重复情况。
然后枚举小于b的a的约数,拿ans减掉就可以了
*/

 

 

现在你有多努力,未来就有多幸运

努力不一定会得到,但不努力就什么都得不到。这个世界并不是掌握在那些无所事事的人手中,而恰恰掌握在能够经历过磨难不断前往的人手中。你所付出的努力,都会有所回报。唯一需要的是坚持,千万不要因为走得太久,而忘记了我们为什么要出发。生活从来不会亏待真正努力的人,幸运只是来得早或晚而已。趁着年轻,就要努力向上,如果你觉得现在走的辛苦,证明你是在走上坡路,离成功只会越来越近。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵩韵儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值