lightoj 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
2

10 2

12 2

Sample Output
Case 1: 1

Case 2: 2
题意:给你一个面积s,和最短边的最小值c,问你有多少组数据(ab=面积)满足题意,但a和b的数据不能小于所给的最小值
很显然就是要你找出s有多少个因子
设s有y个因子,其中小于c的有x个,那所求的组数为ant=(y-2x)/2;
而显然s可以唯一拆成有限个质因子相乘c=p1^ t1 p2^t2+… pn ^tn其他因子都是由有限个质因子相乘得来,即因子的个数y=(t1+1)(t2+1)
(t3+3)+…+(tn+1),显然,s不可能有超过一个大于根号s的质因子(因为如果有2个,那这两个相乘就大于s了),所以只需要找出1e6内的质数就够了。
ac代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// 1010101 > sqrt(1e12)
const int N=1010101;
int s[N/2];
bool p[N];
int k=0,l=1;
	void shusu()
{
	memset(p,false,sizeof(p));
	for(int i=2;i<N;i++)
	{
		if(!p[i])
		{
			s[k++]=i;
			for(int j=2;i*j<N;j++)
			{
				p[i*j]=1;
			 } 
		}
		
	}
}
void asd(long long  x,long long  y)
{
	 if (y> x / y) {    printf("Case %d: 0\n",l); l++;  return;  }
 
	long long z=x;
	int ant,sum=1;
	for(int i=0;i<k&&s[i]<=x;i++)
	{
	
		ant=0;
		if(x%s[i]==0)
		{
			while(x%s[i]==0)
			{
				x/=s[i];
				ant++;	
			}
		}
		sum*=(ant+1);
	}
	if(x>1)sum*=2;
	for(int j=1;j<y;j++)
	{
		if(z%j==0)
		sum-=2;
	}
	printf("Case %d: %d\n",l,sum/2);
	l++;
}
int main()
{
	int n;
	long long int a,b;
	cin>>n;
	shusu();
	while(n--)
	{
		cin>>a>>b;
		asd(a,b);
	}
}在这里插入代码片
发布了109 篇原创文章 · 获赞 35 · 访问量 6032

猜你喜欢

转载自blog.csdn.net/weixin_43965698/article/details/88956317
今日推荐