杭电1239————缩小数据范围的暴力搜索(水题)
2014-08-01 15:23
405 查看
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的我好蛋疼..............这道题做的时候太浮躁了,不想动脑子....反而花费了更多的时间(三个多小时.....我太弱了......)多动脑不会死人的!!不动脑子才会死人...请大家引以为戒...
相关文章推荐
- POJ 2109 水题 加各种数据类型范围
- HDU--杭电--1016--Prime Ring Problem--深度优先搜索--此类中的水题
- 搞笑版费马大定理 (暴力,优化,缩小范围)
- MSDN Visual系列:使用范围细化业务数据目录的搜索
- BSOJ 3022 又一类数字三角形--根据数据范围的优化+背包思想递推/搜索
- 【 TIMUS-1748】【数学+搜索】The Most Complex Number【求反素数,数据范围较大】
- HDU--杭电--1716--排列2--深度优先搜索--此类中的基础题,也是水题
- HDU 1239 Calling Extraterrestrial Intelligence Again 【打表+暴力搜索】
- 学会缩小搜索条件的范围以便在 Outlook 中更好地搜索
- codeforces----2017 Hackatari Codeathon(A 暴力搜索 G概率)(水题DEH)
- 逆袭指数---杭电校赛(暴力搜索)
- 杭电1239—搜索
- 搜索一定范围类的图片数据(类似QQ附近人)
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
- Webpack优化配置缩小文件搜索范围
- Codeforces Round #315 (Div. 2) C 暴力找数据范围
- 杭电 搜索 水题1072 Nightmare
- hdu 4569 Special equations ( 必要条件缩小范围后暴力)
- 【直接生成法】直接生成想要的数据,而不是暴力搜索
- 数据挖掘时用特征缩小范围很重要