LightOJ-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

2

10 2

12 2

Sample Output

Case 1: 1

Case 2: 2


题意:给出a和b,要求求出有多少个两个数相乘等于a,且这两个数必须>=b

思路:利用唯一分解定理,求出有多少个因子个数,然后因子个数/2就是因子对数,再减去不满足条件的即可(不满足条件的:有一个因子数小于b)


唯一分解定理:N = p1^a1*p2^a2*p3^a3* ... *pn^an(其中p1、p2、... pn为N的因子,a1、a2、... 、an分别为因子的指数)

求N的因子个数:sum=(1 + a1)*(1 + a2)*(1 + a3)*...*(1 + an)

 1 #include<stdio.h>
 2 #include<cmath>
 3 #include<string.h>
 4 typedef long long ll;
 5 using namespace std;
 6 
 7 
 8 const int N=1e6+20;
 9 int book[N],p;
10 ll pri[N];
11 
12 void prime()
13 {
14     //非素数标记为1
15     memset(book,0,sizeof(book));
16     book[0]=1;
17     book[1]=1;
18     p=0;
19     for(int i=2; i<N; i++)
20     {
21         if(book[i]==0)
22         {
23             pri[p++]=i;
24             for(int j=i*2; j<N; j+=i)
25                 book[j]=1;
26         }
27     }
28 }
29 
30 ll yinzi(ll a)//求N的因子个数sum=(1 + a1)*(1 + a2)*(1 + a3)*...*(1 + an);
31 {
32     ll ans=0,sum=1;
33     for(ll i=0; i<p&&(pri[i]*pri[i]<=a); i++)
34     {
35         if(a%pri[i]==0)
36         {
37             ans=0;
38             while(a%pri[i]==0)
39             {
40                 ans++;
41                 a/=pri[i];
42             }
43             sum*=(1+ans);
44         }
45     }
46     if(a>1)//说明还剩有1个素数因子没有被算入,根据公式可得
47         sum=sum*(1+1);
48     return sum;
49 }
50 
51 int main()
52 {
53     prime();
54     int t;
55     ll a,b;
56     scanf("%d",&t);
57     int tt=1;
58     while(t--)
59     {
60         scanf("%lld %lld",&a,&b);
61         if(a<b*b)
62         {
63             printf("Case %d: 0\n",tt++);
64             continue;
65         }
66         else
67         {
68             ll ans=yinzi(a);
69             ll duishu=ans/2;
70             for(ll i=1; i<b; i++)
71             {
72                 if(a%i==0)
73                 {
74                     duishu--;
75                 }
76             }
77             printf("Case %d: %lld\n",tt++,duishu);
78         }
79     }
80     return 0;
81 }

猜你喜欢

转载自www.cnblogs.com/OFSHK/p/11329177.html
今日推荐