题目
http://acm.hdu.edu.cn/showproblem.php?pid=1239
Problem Description
A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November 16, 1974. The message consisted of 1679 bits and was meant to be translated to a rectangular picture with 23 * 73 pixels. Since both 23 and 73 are prime numbers, 23 * 73 is the unique possible size of the translated rectangular picture each edge of which is longer than 1 pixel. Of course, there was no guarantee that the receivers would try to translate the message to a rectangular picture. Even if they would, they might put the pixels into the rectangle incorrectly. The senders of the Arecibo message were optimistic.
We are planning a similar project. Your task in the project is to find the most suitable width and height of the translated rectangular picture. The term “most suitable” is defined as follows. An integer m greater than 4 is given. A positive fraction a / b less than or equal to 1 is also given. The area of the picture should not be greater than m. Both of the width and the height of the translated picture should be prime numbers. The ratio of the width to the height should not be less than a / b nor greater than 1. You should maximize the area of the picture under these constraints.
In other words, you will receive an integer m and a fraction a / b. It holds that m > 4 and 0 < a / b < 1. You should find the pair of prime numbers p, q such that pq <= m and a / b <= p / q <= 1, and furthermore, the product pq takes the maximum value among such pairs of two prime numbers. You should report p and q as the “most suitable” width and height of the translated picture.
Input
The input is a sequence of at most 2000 triplets of positive integers, delimited by a space character in between. Each line contains a single triplet. The sequence is followed by a triplet of zeros, 0 0 0, which indicated the end of the input and should not be treated as data to be processed.
The integers of each input triplet are the integer m, the numerator a, and the denominator b described above, in this order. You may assume 4 < m <= 100000 and 1 <= a <= b <= 1000.
Output
The output is a sequence of pairs of positive integers. The i-th output pair corresponds to the i-th input triplet. The integers of each output pair are the width p and the height q described above, in this order.
Each output line contains a single pair. A space character is put between the integers as a delimiter. No other characters should appear in the output.
Sample Input
5 1 2
99999 999 999
1680 5 16
1970 1 1
2002 4 11
0 0 0
Sample Output
2 2
313 313
23 73
43 43
37 53
一、题目大意与分析
1974年11月16日星期六下午,通过波多黎各的阿雷西博射电望远镜,人类向地外智慧发出了一条信息。该消息由1679位组成,打算翻译成23*73像素的矩形图片。由于23和73都是素数,所以23×73是每个边都大于1像素的平移矩形图片的唯一可能大小。当然,并不能保证接收者会试图将信息翻译成一幅矩形图片。即使他们会这样做,他们也可能错误地将像素放入矩形中。阿雷西博消息的发送者很乐观。
我们正在计划一个类似的项目。你在项目中的任务是找到最合适的宽度和高度的翻译矩形图片。术语“最合适”的定义如下。给出了一个大于4的整数m。还给出了小于或等于1的正分数A/b。图片的面积不应大于m。翻译图片的宽度和高度都应为素数。宽高比不应小于a/b,也不应大于1。在这些限制条件下,你应该最大化图片的面积。
换句话说,你将得到一个整数m和一个分数a/b,它认为m>4和0<a/b<1。你应该找到一对素数p,q,使得pq<=m和a/b<=p/q<=1,而且乘积pq取这对素数中的最大值。你应该报告p和q为“最合适”的宽度和高度的翻译图片。
==题目大致意思是给你m,a,b三个数,要你找出一对最大的p和q,使得pq<=m和a/b<=p/q<=1。我是在做搜索题刷到的,一直再想搜索该怎么做最好,后来发现,依据题目所给范围来看(4 < m <= 100000 and 1 <= a <= b <= 1000)p或者q不可能会超过10000,给的范围小,所以这题只需要筛选出素数然后枚举即可
素数筛选点这
二、代码
代码如下(示例):
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 10005
using namespace std;
int prime[maxn],m,a,b,num=0;
int vis[maxn];
void Erathosthenes()//筛选素数
{
int n=10000;
memset(vis,0,sizeof(vis));
for(int i=2; i<=n; i++)
if(!vis[i])
{
prime[num++]=i;
for(int j=i*i; j<=n; j+=i)
vis[j]=i;
}
}
int main()
{
double s;
int i,j;
int p,q,sum;
Erathosthenes();//考虑到所求素数不可能大于10000,所以直接筛选素数
while(scanf("%d%d%d",&m,&a,&b)==3)
{
if(m==0&&a==0&&b==0)
break;
double s=(double)a/b;
sum=0;
for(i=num-1;i>=0;i--)
{
for(j=i;j>=0;j--)
{
if(prime[i]>m||prime[j]*prime[i]>m||((double)prime[j]/prime[i])<s)//不符合条件
continue;
if(prime[i]*prime[j]>sum)//要取最大
{
sum=prime[i]*prime[j];
p=prime[i];
q=prime[j];
}
}
}
printf("%d %d\n",q,p);
}
return 0;
}