2.Calling Extraterrestrial Intelligence Again

来源:互联网 发布:搜索引擎客户数据分析 编辑:程序博客网 时间:2024/05/22 17:29

Calling Extraterrestrial Intelligence Again

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.

 输入:输入若干行数据,当某行输入0 0 0时,输入结束。每行数据如下:给定一个整数m( 4 < m <= 100000),分子a,分母b(1 <= a <= b <= 1000),要求找到两个素数pq,使得p*q<=m,  a / b <= p / q <= 1并且找到的这对pq是所有符合上述条件的pq中使得p*q达到最大值的。

 

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




暴力代码 (很悲剧,暴力代码的复杂度实在是太高了,结果只能通过第一组数值较小的数据,m一旦比较大,就跑不出结果)

/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"int m=1680,a=5,b=16,p1=1000,q1=1000;int Shus(int n){int i ,sum=n;int a = (int)sqrt(n);for(i=2;i<=a;i++){if(sum%i==0){return 0;}}return 1;}void dfs(int p ,int q){if(p*q>m)return ;if(p>100||q>100)return ;else{if(Shus(p)==1 && Shus(q)==1){if( (a*1.0/b)<=(p*1.0/q)&&(p*1.0/q)<=1){if( abs(m-(p*q)) <abs(m-(p1*q1)) ){p1=p;q1=q;}}}}dfs(p+1,q);dfs(p,q+1);}void main(){    dfs(20,);    printf(" %d %d ",p1,q1);}




优化方案:先求出所有满足m的素数,然后再两两求出最大pq,从最大的素数开始求


用自己的方法,只能让 pq取值最小在【2,,50000】之间  (我给它取名为最小测探法   既以最小的结果n来测探是否大于目标数m,如果大于,则范围缩小)

方法如下   : 小于100000的素数,最大为99991     最小为2  2*99991 >100000   2*800000  2*70000  2*60000 都同理,所以都不能取

一直到 2*50000<=100000 所以 p,q的取值范围缩小到【2,50000】


事实上 p,q 的取值能再次缩小

由 1《=a《=b《=1000   和   a / b <= p / q <= 1   可知  p/q要≥a/b    p/q就得≥a/b的最小值   而a/b的最小值肯定为 a取最小  b取最大   根据1《=a《=b《=1000  可以知道  a最小为1   b最大为1000  所以a/b的最小值为   a/b=1/1000    可得 p/q≥1/1000  根据题目的测试数据,p最小可以取到2,所以,当p=2  2/q≥1/1000时,q最大只能取到2000,所以p,q的取值范围为 【2,2000】

/* Note:Your choice is C IDE */#include "stdio.h"#include "math.h"int m=1680,a=5,b=16;int num[2000];int q1=1000,p1=1000;int Shus(int n){int i ,sum=n;int a = (int)sqrt(n);for(i=2;i<=a;i++){if(sum%i==0){return 0;}}return 1;}void dfs(int p){int i ,j=p;//最后一格没有入肉for(i=j-1;i>=0;i--){if(num[i]*2>m)continue;for(j=i;j>=0;j--){if( (a*1.0/b)<=(num[i]*1.0/num[j])   &&  (a*1.0/b)<=1){if(num[i]*num[j]<=m)   //p*q<=m{if(abs(m-(num[i]*num[j]))<abs(m-(q1*p1))){q1 = num[j];p1 =num[i];}}}}}}void main(){int i;int j=0;    for(i=1;i<=2000;i++)//求2到2000之间的素数    {    if( Shus(i)==1 )    {     num[j]=i;    j++;    //printf(" %d ",num[i]);    }    }    dfs(j);    printf(" %d %d ",q1,p1);}



 

0 0
原创粉丝点击