【Lightoj】1341

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

2
10 2
12 2

Output for Sample Input

Case 1: 1
Case 2: 2

题意:

给出整数 a 和 b ,求区间[b, a] 内的 a 的约数对的个数,即:满足c*d == a &&c>=b,d>=b。a 的约数对(比如[2, 3] 与 [3, 2] 为同一对)。

题解:

先素数打表求出有那些数不是素数,接下来就通过算术基本定理
算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3……Pnan,这里P1小于P2小于P3……小于Pn均为质数,其中指数ai是正整数。这样的分解称为 N 的标准分解式
这样就能得到(1,a)所有的正因数个数,除以二就是总的,然后遍历(1,b)的正因数个数,最终答案就是总的减去b之前的。

代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
typedef long long ll;
const int Max=1000050;
ll k=0;
ll a,b;
int prim[Max],p[Max];
void sieve()          //素数打表
{
     k=0;
    memset(p,0,sizeof(p));
    for(int i=2;i<=Max;i++)
    {
        if(!p[i])
        {
            prim[k++]=i;
            for(int j=2*i;j<=Max;j+=i)
                p[j]=1;
        }
    }
}
ll cont(int x)  
{
   ll s=1;
   ll i=0;
   while(prim[i]<a&&i<k)
   {
       int c=0;
       while(a%prim[i]==0)
       {
           a/=prim[i];
           c++;
       }
       s*=(c+1);
       i++;
   }
   if(a>1)    //如果a不能被整分,说明还有一个素数是它的约数,此时c=1 
    s*=2;
   return s;
}
int main()
{
    int t;
    sieve();
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        ll s=0;
        scanf("%lld%lld",&a,&b);
        if(b*b>a)
           printf("Case %d: %d\n",i,0);
        else
        {
            for(int j=1;j<b;j++)
            {
                if(a%j==0)
                   s++;
            }
            ll n=cont(a)/2;
            printf("Case %d: %lld\n",i,n-s);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lhhnb/article/details/80070654