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

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 beforegetting the Magical Lamp which summons a powerful Genie. Here we are concernedabout the first mystery.

Aladdin was about to enter to a magical cave, led by theevil sorcerer who disguised himself as Aladdin's uncle, found a strange magicalflying carpet at the entrance. There were some strange creatures guarding theentrance of the cave. Aladdin could run, but he knew that there was a highchance of getting caught. So, he decided to use the magical flying carpet. Thecarpet was rectangular shaped, but not square shaped. Aladdin took the carpetand with the help of it he passed the entrance.

Now you are given the area of the carpet and the length ofthe minimum possible side of the carpet, your task is to find how many types ofcarpets are possible. For example, the area of the carpet 12, and the minimumpossible side of the carpet is 2, then there can be two types of carpets andtheir 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: ab (1 ≤ b ≤ a ≤ 1012) where adenotes the area of the carpet and b denotes the minimum possible sideof the carpet.

Output

For each case, print the case number and the number ofpossible carpets.

Sample Input

Output for Sample Input

2

10 2

12 2

Case 1: 1

Case 2: 2

题目分析:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个数为num = (1 + a1) * (1 + a2) *...(1 + ai),这里的ai是素因子的指数,见唯一分解定理,因为题目说了不会存在c==d的情况,因此num要除2,去掉重复情况,然后枚举小于b的a的约数,拿num减掉就可以了

#include <stdio.h>
#include <math.h>
#include <string.h>
#define maxn 1000010
typedef long long ll;
int prime[maxn],cnt;
bool u[maxn];
ll a,b,num,p[maxn];
void prime_q()//预处理出质数 
{
	memset(u,false,sizeof(u));
	cnt=1;
	for(int i=2;i<=sqrt(maxn);i++)
	{
		if(!u[i])
		{
			for(int j=i*i;j<=maxn;j+=i)
					u[j]=true;
		}
	}
	for(int i=2;i<=maxn;i++)
	{
		 
			if(!u[i]) 
			p[cnt++]=i;
	}
}
void qwe(ll z)// 求约数个数 
{
	 num=1;
	for(int i=1;i<cnt&&p[i]*p[i]<=z;i++)
	{
		int cc=0;
		 
	 
			while(z%p[i]==0)
			{
				z=z/p[i];
				cc++;
			}
	 
		num*=cc+1;
	} 
	if(z>1) num*=2;
}
int main()
{
	prime_q();
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++)
	{
		scanf("%lld%lld",&a,&b);
		if(a<b*b)
		{
			printf("Case %d: 0\n",i);
		}
		else
		{
			ll h=a;
			qwe(h);
		 	
			num=num/2;
			for(ll j=1;j<b;j++)
			{
				if(a%j==0) num--;
			}
			printf("Case %d: %lld\n",i,num);
		}
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/qq_42434171/article/details/82023409