杭电1239————缩小数据范围的暴力搜索(水题)

本文探讨了如何在限定的条件下找到最合适的矩形图片尺寸,包括宽度和高度均为质数,且面积不超过给定值,同时宽度与高度的比例在特定范围内。通过算法优化,避免了直接遍历所有可能组合的复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Calling Extraterrestrial Intelligence Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4447    Accepted Submission(s): 2319


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
条件 1:图片面积不能比area大 2:图片的width和hight必须都是质数 3:wight / hight >= a / b  && wight / hight <=1  在以上条件下要求面积尽可能的大 p q从1到100000循环挨个试一下这样的话是肯定超时的,于是想缩小数据的范围 思考: 1.p * q <= 1000000 并且pq都得是素数,最小的素数是2 那么p.q的值肯定都小于 50000 2.1<=a<=b<=1000   则min(a/b) == 0.001 我们想尽量的缩小pq的范围,(50000 * 50000的循环肯定超时了)  通过2,我们做一个推测 当p == 5 时(50000 * 0.001) q<=20000 就能保证 p * q <= 100000 当p == 10时(10000 * 0.001) q<=10000 就能保证 p * q <= 100000 那么p.q的范围都不能大于10000 #include <stdio.h> #include <stdlib.h> #include <math.h> #define maxn 10000 int area,a,b,count,width,hight; int maxarea;/*最大面积即结果*/  int prime[maxn] = {0};/*存放10000以内的所有素数*/ int get_prime() { int num[maxn]; for(int i = 0 ;i < maxn ; i ++) num[i] = i; num[1] = 0;/*1不是素数*/  for(int i = 2 ; i <= sqrt((double)maxn) ; i++) { for(int j = i+1 ; j < maxn ; j++) { if (num[j] != 0 && num[j] % i == 0)                 num[j]=0;             //如果num[j]中存的数字是i的倍数的话,那么肯定不是质数,于是就把num[j]置空  } /*筛选法求素数*/  int count = 0; for(int i = 0 ; i < maxn ;i ++) { if(num[i] != 0) prime[count++] = i; } return count; } int check(int x,int y) { int flag = 0; if( (prime[y] * b >= prime[x] * a)&& prime[x] * prime[y] <= area) flag = 1; return flag; } int main() { count = get_prime();/*count表示10000以内素数的个数*/  while(scanf("%d%d%d",&area,&a,&b)) { if(area == 0 && a == 0 && b == 0) break; int total = count; maxarea = 0; for(int i = total - 1 ; i >= 0 ; i -- ) { for(int j = i ; j >= 0 ; j --) { if(check(i,j) && prime[i] * prime[j] > maxarea) { maxarea = prime[i] * prime[j]; width = prime[i]; hight = prime[j]; } } } printf("%d %d\n",hight,width); } return 0; }
PS:提交的时候CE了好几次,sqrt系统内置要求是double类型的。我用Dec_cpp不需要强制转换类型就可以,但是杭电要求转换..CE的我好蛋疼..............这道题做的时候太浮躁了,不想动脑子....反而花费了更多的时间(三个多小时.....我太弱了......)多动脑不会死人的!!不动脑子才会死人...请大家引以为戒...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值