LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理

题目链接:https://vjudge.net/problem/LightOJ-1341

 

1341 - Aladdin and the Flying Carpet
    PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

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

 

 

 

 

题意:

求n有多少对因子(即a*b=n,则{a,b}为其中一对,不要求顺序),且因子的最小下限为m?

 

题解:

1.求因子个数,则对n进行质因子分解。

2.先不考虑因子的最小下限m,则n共有 ∏(num[i]+1)个因子,其中num[i]为第i个质因子的个数。

3.当n是平方数的时候,除了sqrt(n)之外,其他因子是一一对应的,即a*b=n, a!=b; 当n不是平方数时,显然它的因子是一一对应的,即有2*pair个因子,所以有pair对因子, 因此 pair = (∏(num[i]+1))/2,而题目说明了n非平方数。

4.再考虑回下限m:可知当 a*b = n 时, a、b必定满足 a<=sqrt(n)或b<=sqrt(n)。所以当m>sqrt(n)时,就不存在这样的因子对。当m<=sqrt(n)时,可以直接枚举较小的那个因子,枚举范围为:1~min(m, sqrt(n)+1),如果它能整除n,这表明存在一对不满足最小下限的因子对,删除即可。

 

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e6+10;
18 
19 LL n, m;
20 bool notprime[MAXN];
21 int prime[MAXN+1];
22 void getPrime()
23 {
24     memset(notprime, false, sizeof(notprime));
25     notprime[0] = notprime[1] = true;
26     prime[0] = 0;
27     for (int i = 2; i<=MAXN; i++)
28     {
29         if (!notprime[i])prime[++prime[0]] = i;
30         for (int j = 1; j<=prime[0 ]&& prime[j]<=MAXN/i; j++)
31         {
32             notprime[prime[j]*i] = true;
33             if (i%prime[j] == 0) break;
34         }
35     }
36 }
37 
38 int fatCnt;
39 LL factor[100][2];
40 int getFactors()
41 {
42     LL tmp = n;
43     fatCnt = 0;
44     for(int i = 1; prime[i]<=tmp/prime[i]; i++)
45     {
46         if(tmp%prime[i]==0)
47         {
48             factor[++fatCnt][0] = prime[i];
49             factor[fatCnt][1] = 0;
50             while(tmp%prime[i]==0) tmp /= prime[i], factor[fatCnt][1]++;
51         }
52     }
53     if(tmp>1) factor[++fatCnt][0] = tmp, factor[fatCnt][1] = 1;
54 }
55 
56 int main()
57 {
58     getPrime();
59     int T, kase = 0;
60     scanf("%d", &T);
61     while(T--)
62     {
63         scanf("%lld%lld", &n,&m);
64         int ans = 1;
65         if(1LL*m*m>n)
66             ans = 0;
67         else
68         {
69             getFactors();
70             for(int i = 1; i<=fatCnt; i++)
71                 ans *= (factor[i][1]+1);
72             ans /= 2;
73             for(LL i = 1; i<min(m,(LL)sqrt(n)+1); i++)
74                 if(n%i==0) ans--;
75         }
76         printf("Case %d: %lld\n", ++kase, ans);
77     }
78 }
View Code

 

posted on 2018-01-26 16:38  h_z_cong  阅读(220)  评论(0编辑  收藏  举报

导航