Light OJ 1341 Aladdin and the Flying Carpet(算数基本定理)

来源:互联网 发布:政府网站域名后缀 编辑:程序博客网 时间:2024/06/06 09:23

题目链接:http://lightoj.com/volume_showproblem.php?problem=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

Output for Sample Input

2

10 2

12 2

Case 1: 1

Case 2: 2




题意

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

PS:

先素数打表一下,然后再运用算术基本定理中的(1)



即可求出正因数的个数,然后再除以2,便是对数,最后再暴力求解出[1,b]中 a 的正因数个数,相减便是答案!

代码如下:

[cpp] view plain copy
print?
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <cmath>  
  4. #include <iostream>  
  5. #include <algorithm>  
  6. using namespace std;  
  7. #define maxn 1000047  
  8. #define LL long long  
  9. LL prim[maxn], p[maxn];  
  10. int k = 0;  
  11. void find_prim()  
  12. {  
  13.     k = 0;  
  14.     for(LL i = 2; i <= maxn; i++)  
  15.     {  
  16.         if(!p[i])  
  17.         {  
  18.             prim[k++] = i;  
  19.             for(LL j = i+i; j <= maxn; j+=i)  
  20.             {  
  21.                 p[j] = 1;  
  22.             }  
  23.         }  
  24.     }  
  25. }  
  26. LL cont(LL a)  
  27. {  
  28.     LL s = 1;  
  29.     if(a == 0)  
  30.     {  
  31.         return 0;  
  32.     }  
  33.     LL tt = 0;  
  34.     LL i = 0;  
  35.     while(prim[i] < a && i < k)  
  36.     {  
  37.         tt = 0;  
  38.         if(a%prim[i] == 0)  
  39.         {  
  40.             while(a%prim[i] == 0)  
  41.             {  
  42.                 a/=prim[i];  
  43.                 tt++;  
  44.             }  
  45.         }  
  46.         s = tt+1;  
  47.         i++;  
  48.     }  
  49.     if(a > 1)  
  50.     {  
  51.         s = 1+1;//一次  
  52.     }  
  53.     return s;  
  54. }  
  55. int main()  
  56. {  
  57.     LL a, b;  
  58.     int t;  
  59.     int cas = 0;  
  60.     find_prim();  
  61.     scanf(”%d”,&t);  
  62.     while(t–)  
  63.     {  
  64.         scanf(”%lld%lld”,&a,&b);  
  65.         int cnt = 0;  
  66.         LL num = 0, ans;  
  67.         if(b >= sqrt(a))  
  68.             ans = 0;        // b大小限定  
  69.         else  
  70.         {  
  71.             for(LL i = 1; i < b; i++) //暴力枚举[1, b]中a的约数  
  72.             {  
  73.                 if(a%i == 0)  
  74.                 {  
  75.                     cnt++;  
  76.                 }  
  77.             }  
  78.             num = cont(a)/2;  
  79.             ans = num - cnt;  
  80.         }  
  81.         printf(”Case %d: %lld\n”,++cas,ans);  
  82.     }  
  83.     return 0;  
  84. }  

阅读全文
0 0
原创粉丝点击