您的位置:首页 > 大数据 > 人工智能

hdu 1239 Calling Extraterrestrial Intelligence Again

2014-04-09 22:16 459 查看
Calling Extraterrestrial Intelligence Again



Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4205 Accepted Submission(s): 2207





Problem Description

A message from humans to extraterrestrialintelligence was sent through the Arecibo radio telescope in Puerto Rico on theafternoon of Saturday November 16, 1974. The message consisted of 1679 bits andwas meant to be translated to a rectangular picture with
23 * 73 pixels. Sinceboth 23 and 73 are prime numbers, 23 * 73 is the unique possible size of thetranslated rectangular picture each edge of which is longer than 1 pixel. Ofcourse, there was no guarantee that the receivers would try to translate themessage
to a rectangular picture. Even if they would, they might put the pixelsinto the rectangle incorrectly. The senders of the Arecibo message wereoptimistic.

We are planning a similar project. Yourtask in the project is to find the most suitable width and height of thetranslated rectangular picture. The term "most suitable" is definedas follows. An integer m greater than 4 is given. A positive fraction a / bless
than or equal to 1 is also given. The area of the picture should not begreater than m. Both of the width and the height of the translated pictureshould be prime numbers. The ratio of the width to the height should not beless than a / b nor greater than 1.
You should maximize the area of the pictureunder these constraints.



In other words, you will receive an integerm and a fraction a / b. It holds that m > 4 and 0 < a / b < 1. Youshould 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 amongsuch
pairs of two prime numbers. You should report p and q as the "mostsuitable" width and height of the translated picture.





Input

The input is a sequence of at most 2000triplets of positive integers, delimited by a space character in between. Eachline 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 datato be processed.



The integers of each input triplet are theinteger m, the numerator a, and the denominator b described above, in thisorder. You may assume 4 < m <= 100000 and 1 <= a <= b <= 1000.





Output

The output is a sequence of pairs ofpositive 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 describedabove, in this order.



Each output line contains a single pair. Aspace character is put between the integers as a delimiter. No other charactersshould 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



题解:

穷举,先退出q <= sqrt(m*b/a),然后列举就可以了。

源代码:

#include
<iostream>
#include
<string.h>
#include
<string>
#include
<stdio.h>
#include
<math.h>
#include
<algorithm>
#define
MAX100001

using namespacestd;

bool prime[100001];
void cal_prime()
{
doubletp;
prime[0]= prime[1] = false;
prime[2]= true;
intp;
boolflag =
true;
intnum = 0;
for(inti = 3;i <
MAX;i++)
{
tp= sqrt(i*1.0);
p =(int)tp;
flag= true;
for(intj = 2;j <= p;j++)
{
if(i%j)
continue;
else
{
flag= false;
break;
}
}
if(flag)
prime[i]= flag;
}
}

int main()
{
cal_prime();
intn,a,b,q,p,mx,ansq = 0,ansp = 0;
while(scanf("%d%d%d",&n,&a,&b)!=
EOF && n &&a && b)
{
mx= 0;
q =int(sqrt((1.0*n*b)/(a*1.0)))+1;
while(!prime[q])
q--;

for(inti = q;i >= 0;i--)
{
if(!prime[i])
continue;
for(intj = i;j >= 0;j--)
{
if(!prime[j])
continue;
if((1.0*a)/(1.0*b)<= (1.0*j)/(1.0*i) && j*i <= n && mx < i*j)
{
mx= i*j;
ansp= j;
ansq= i;
}
}
}
printf("%d %d\n",ansp,ansq);
}

return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: