【数论】穹妹的求助

D e s c r i p t i o n Description

由于穹妹很聪明,她的数学老师给她布置了一个作业,让她求出L到R之间不同因子数最多的那个数和这个数不同的因子个数(包含1和本身)。这个无聊的数学老师给出的L和R区间可能比较大,穹妹不想浪费时间在这种简单的问题上,她认为宝贵的时间应该更多的利用在和哥哥相处上,所以她向精通数学和计算机的你求助,希望你能帮她解决这个问题。

I n p u t Input

第一行:两个正整数L和R,保证L<=R

O u t p u t Output

一行两个数,第一个数表示L到R之间因子数最多的那个数,第二个数表示该数的因子个数(包含1和本身)

S a m p l e I n p u t Sample Input

1000 2000

S a m p l e O u t p u t Sample Output

1680 40

T r a i n Train o f of T h o u g h t Thought

24的因子为:
1           24
2   		12
4			6
3			8

只要求出左边就可以推算出右边(因数都是成对出现的)
不过也有例外:
4 4 = 16 4*4=16
这里只需要记一次就可以了

C o d e Code

#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
long long l,r,ans,maxx,ans2;
int main()
{
	scanf("%lld%lld",&l,&r);
	for (long long i=l; i<=r; ++i)
	{
		long long tt=sqrt(i);
	 	for (long long j=1; j<=tt; ++j)
	 	{
	 		if (i%j==0 && j*j!=i) ans+=2;//一对的情况
	 	 		else if (i%j==0) ans++;//单个的情况
	 	}
	 	if (maxx<ans) maxx=ans,ans2=i;//记录
	 	ans=0;
	}
	printf("%lld",ans2);
	printf(" %lld",maxx);
}

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/89069131