其他分享
首页 > 其他分享> > Aladdin and the Flying Carpet

Aladdin and the Flying Carpet

作者:互联网

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的矩形有几种。

唯一分解定理
每一个大于1的正整数均可分解为有限个素数的积,如果不计素因数在乘积中的次序,则分解方式是唯一的。将n的素因数分解中相同的素因子收集到一起,可只每个大于1的正整数n可唯一地写成 n = p1^a1+p2 ^a2+p3 ^a3…pk ^ak,其中,p1,p2,p3,…,pk ,是互不相同的素数,而a1,a2,a3,…,ak 是正整数,上面的分解式称为n的标准分解。

性质: n的正约数个数,τ(n) = (1+a1)(1+a2)(1+a3)…(1+ak) ;n的正约数之和,σ(n)=(1+p1+…+p1^a1)(1+p2+…+p2 ^a2)…(1+pk+…+pk ^ak)

唯一分解定理来自这里

例:12->4*3->2^2 *3 ,所以12的正约数个数为(1+2) * (1+1)[次方数分别为2,1]=6,(1,2,3,4,6,12共6个)

AC代码:

#include<stdio.h>
#include<string.h>
long long n=1000010,book[1000010],s[1000010],k;
void su()//素数打表的两种方法
{
    /*long long i,j;
    k=0;
    for(i=2;i<n;i++)
    {
        if(book[i])
        {
            s[k++]=i;
            for(j=2;i*j<=n;j++)
                book[i*j]=1;
        }
    }*/
	long long i,j;
	k=0;
	for(i=2;i<n;i++)
	{
		if(book[i]==0)
		{
			s[k++]=i;
			for(j=2*i;j<n;j=j+i)
			book[j]=1;
		}	
	}
}
long long f(long long a)//唯一分解定理
{
	long long i,m=1;
	for(i=0;i<k&&s[i]*s[i]<=a;i++)
		long long ans=0;
		while(a%s[i]==0)
		{
			ans++;//记录次方
			a=a/s[i];
		}
		m=m*(1+ans);//计算不同因数的个数
	}
	if(a>1)//说明还剩一个数,即还剩一个次方,此时ans=1,
	m=m*2;
	return m;
}
int main()
{
	long long a,b,t,i,sum,z=0;
	su();
	scanf("%lld",&t);
	while(t--)
	{
		z++;
		sum=0;
		scanf("%lld%lld",&a,&b);
		if(a<=b*b)
		printf("Case %lld: 0\n",z);
		else
		{
			sum=f(a)/2;// 除2的原因是成对的找,即找能组成几个长方形
			for(i=1;i<b;i++)//因为是在b之后寻找,所以要减去b之前的素数因子的个数(对数)
			{
				if(a%i==0)
				sum--;
			}
			printf("Case %lld: %lld\n",z,sum);
		 } 		 
	}
	return 0;
}

标签:12,Aladdin,sum,Flying,possible,long,Carpet,carpet
来源: https://blog.csdn.net/Anthcony/article/details/117627748