当前位置: 代码迷 >> 综合 >> LightOJ 1341Aladdin and the Flying Carpet(唯一分解定理)
  详细解决方案

LightOJ 1341Aladdin and the Flying Carpet(唯一分解定理)

热度:16   发布时间:2023-12-06 19:51:57.0

题目链接: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



题意:

 有一个矩形的毯子,已知它一定不是正方形,并且已知最短边和毯子的面积,求这个毯子可能有多少种形状,宽必须大于等于给出的最短边。

分析:

首先,如果最短的边大于等于面积的平方根,两个大于面积平方根的边不可能组成面积等于给定面积的矩形。

所以这种情况是0. 然后最短边的范围就变成了1-1e6,问题也变成了在最短边(min_side)到面积(area)这些数中有多少对不同的数相乘等于面积(area)。可以转变为1-area中不同的数相乘为area的对数,再减去1-min_side-1中不同的数相乘为area的对数。然后利用算术基本定理中的(1)算出1-area中的正因数的个数,正因数关于sqrt(area)对称,每对称的两个相乘是area,因此只要因数的个数除以二就行啦,向下取整,正好也不会算上平方根。再就是1-min_side-1中的对数,暴力就行啦。


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = 1000010;
int prim[80000], p;
bool vis[N];
void init()
{for(int i = 2; i < N; i++)if(!vis[i]){prim[p++] = i;for(int j = i+i; j < N; j += i)vis[j] = 1;}
}
int main()
{init();int T;scanf("%d", &T);for(int kase = 1; kase <= T; kase++){ll area, min_side;scanf("%lld%lld", &area, &min_side);if(min_side >= sqrt(area)){printf("Case %d: %d\n", kase, 0);continue;}ll tmp = area;int ans = 1;for(int i = 0; i < p && 1LL*prim[i]*prim[i] <= area; i++){int cnt = 0;while(area % prim[i] == 0){area /= prim[i];cnt++;}ans *= (cnt+1);}if(area != 1)ans <<= 1;ans >>= 1;for(int i = 1; i < min_side; i++)if(tmp % i == 0)ans--;printf("Case %d: %d\n", kase, ans);}return 0;
}


  相关解决方案