HDU 1239 Calling Extraterrestrial Intelligence Again(找素数)

http://acm.hdu.edu.cn/showproblem.php?pid=1239

题目很长,长就不说了,还是英文,这么长的英文也就算了,居然还是水题,我靠,坑嗲啊!!!

题意:输入 m   a   b 三个数

输出:p   q  
要求:    p  q 是素数,且    p*q <=m   是   <=       ,,且  a/b <=  p/q  <=  1 

4 < m <= 100000 and 1 <= a <= b <= 1000.

  p,q的范围其实可在2—50000(why?)
然而,这是最小的范围吗?
考虑大于10000的某个质数,不妨设为Q,另一个质数为P,则:
1、如果P<10,P/Q<0.001
2、如果P>10,P*Q>100000
而考虑到a,b的取值范围(1<=a<=b<=1000)
可知min(a/b)=0.001
同时,要求: p*q<=m<=100000
所以无论如何质数都不能超过10000。(事实上,不会超过9091)

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int l = 0;
int prim[10005] = {1}, s[10005];
void prime()
{
	int i, j;

	memset(prim, 0, sizeof(prim));

	for (i = 2; i < 10005; i++)
	{
		if (prim[i])
		{
			continue;
		}

		for (j = i + i; j < 10005; j += i)
		{
			prim[j] = 1;
		}

		s[l++] = i;
	}
}

int main()
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	prime();
	int m;
	double a , b;
	while (~scanf("%d %lf %lf", &m, &a, &b) && (m != 0 && a + b != 0))
	{
		int p = 0, q = 0;
		double x = a / b, y;
		int i, j;

		for (i = 0; i < l; i++)
		{
			if (s[i] > m)
			{
				break;
			}
			else
			{

				for (j = i; j<l ; j++)
				{
					y = (double)s[i] / s[j];

					if (y >= x && s[i]*s[j] > q * p && s[i]*s[j] <= m)
					{
						p = s[i];
						q = s[j];
					}
				}
			}
		}
		printf("%d %d\n", p, q);
	}
}


你可能感兴趣的:(REST)