Aladdin and the Flying Carpet LightOJ

来源:互联网 发布:陕西网络创新研究院 编辑:程序博客网 时间:2024/05/22 12:32
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

210 212 2

Sample Output

Case 1: 1Case 2: 2

题意:根据唯一分解定理,把X写成若干素数相乘的形式,则X的正因数的个数为:(1+a1)(1+a2)(1+a3)…(1+an)。(ai为指数)
因为这道题目是求矩形,所以知道一个正因数后,另一个正因数也就确定了,所以每组正因数重复计算了两遍,需要除以2。
最后减去小于b的因数。
其中最主要的是,唯一分解定理:任何大于1的自然数,都可以唯一分解成有限个质数的乘积。
由此打表求10^6以内的质数,求X正因数的个数。

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#define N 1000000int prim[1001000];int vis[1001000];int count = 0,n;void getprim(){    memset(vis,0,sizeof(vis));    for(int i=2;i<=sqrt(N);i++)    {        if(!vis[i])        {           for(int j=i*i;j<=N;j+=i)///因为不能是正方形,所以标记是正方形的情况           vis[j] = 1;        }    }    for(int i=2;i<=N;i++)    {         if(!vis[i])           prim[count++] = i;    }}int main(){    int T;    int t;    getprim();    scanf("%d",&T);    for(t=1;t<=T;t++)    {        long long a,b;        scanf("%lld %lld",&a,&b);        long long x=a;        if(a<=b*b) {printf("Case %d: 0\n",t);continue;}        long long ans = 1;        for(int i=0;i<count&&prim[i]*prim[i]<=a;i++)        {            if(a%prim[i]==0)            {                long long num = 0;                while(a%prim[i] == 0)///寻找素数中a的因数                {                    num++;                    a/=prim[i];                }                ans *=(1+num);///根据唯一分解定理,计算X的因子个数;            }        }        if(a>1) ans*=2;        ans/=2;        for(long long i=1;i<b;i++)///减去小于b的边长的可能;        if(x%i==0)        ans--;         printf("Case %d: %lld\n",t,ans);    }    return 0;}
原创粉丝点击