Aladdin and the Flying Carpet
Problem
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
题意:
给出矩形的面积和组成该矩形的边的最小值,问能组成多少满足条件的矩形。
例如样例12 和 2,共有两种(2,6)和(3,4)。
思路:
唯一分解定理(算术基本定理):任何一个大于1的自然数N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积。
N
=
P
1
a
1
∗
P
2
a
2
∗
P
3
a
3
.
.
.
P
n
a
n
N=P1 ^{a1}*P2^{a2}*P3^{a3}...Pn^{an}
N=P1a1∗P2a2∗P3a3...Pnan
这里P1<P2<P3…<Pn均为质数,其中指数ai是正整数。这样的分解称为N的标准分解式。
①它的正因数的个数为(1+a1)(1+a2)(1+a3)…(1+an)。
②它的全体正因数之和为
s
u
m
=
(
1
+
p
1
+
p
1
2
+
.
.
.
+
p
1
a
1
)
(
1
+
p
2
+
p
2
2
+
.
.
.
p
2
a
2
)
.
.
.
(
1
+
p
n
+
p
n
2
+
.
.
.
+
p
n
a
n
)
sum=(1+p1+p1^{2}+...+p1^{a1})(1+p2+p2^{2}+...p2^{a2})...(1+pn+pn^{2}+...+pn^{an})
sum=(1+p1+p12+...+p1a1)(1+p2+p22+...p2a2)...(1+pn+pn2+...+pnan)
这个题使用唯一分解定理。先进行素数打表,根据打表后的素数算出多少次方,然后代入公式,求出a的正因数个数。在暴力求出1-b内的a的因子个数,相减一下就好了。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
int prime[maxn];
bool is_Prime[maxn];
int tot = 0;
void GetPrime()
{
memset(is_Prime, false, sizeof(is_Prime));
for(int i = 2; i <= maxn ; i++) {
if(!is_Prime[i])
prime[tot++] = i;
for(int j = i+i; j < maxn; j+=i) {
is_Prime[j] = 1;
}
}
}
ll solve(ll x)
{
ll sum = 1;
for(int i = 0; i < tot && prime[i]*prime[i] <= x; i++) {
if(prime[i]>maxn) break;
if(x % prime[i] == 0) {
ll cnt = 0;
while(x % prime[i] == 0) {
cnt++;
x /= prime[i];
}
sum *= (cnt+1);
}
}
if(x > 1)
sum *= 2;
return sum;
}
int main()
{
int t;
scanf("%d", &t);
GetPrime();
for(int i = 1; i <= t; i++) {
ll a, b;
scanf("%lld%lld", &a, &b);
if(b >= sqrt(a))
printf("Case %d: 0\n",i);
else {
ll count = solve(a) / 2;
for(int j = 1; j < b; j++) {
if(a % j == 0)
count--;
}
printf("Case %d: %lld\n",i,count);
}
}
return 0;
}