首页 > ACM题库 > HDU-杭电 > HDU 1239 Calling Extraterrestrial Intelligence Again-数论-[解题报告] C++
2013
12-04

HDU 1239 Calling Extraterrestrial Intelligence Again-数论-[解题报告] C++

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.

输入:

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.

输出:

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.

样例输入:

5 1 2
99999 999 999
1680 5 16
1970 1 1
2002 4 11
0 0 0

样例输出:

2 2
313 313
23 73
43 43
37 53

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1239

一道水题,素数筛选即可

#include<iostream>
 #include<cmath>
 const int N=11000;
 using namespace std;
 bool mark[N];
 int prime[N];
 
 //素数筛选
 void sieve_prime(){
     memset(mark,true,sizeof(mark));
     mark[0]=mark[1]=false;
     for(int i=2;i<sqrt(N*1.0);i++)if(mark[i]){
         for(int j=i*i;j<N;j+=i){
             mark[j]=false;
         }
     }
 }
     
 int main(){
     int m,a,b;
     while(scanf("%d%d%d",&m,&a,&b)!=EOF){
         if(m==0&&a==0&&b==0)break;
         double temp=a*1.0/b;
         sieve_prime();
         int k=0,max=0,p=2,q=2;
         for(int i=2;i<N;i++)if(mark[i]){
             prime[k++]=i;
         }
         for(int i=0;i<k;i++){
             for(int j=i;j<k;j++){
                 if(prime[i]*prime[j]>max&&prime[i]*prime[j]<=m&&prime[i]*1.0/prime[j]>=temp){
                     p=prime[i];
                     q=prime[j];
                     max=prime[i]*prime[j];
                 }
                 if(prime[i]*prime[j]>m)break;
             }
         }
         printf("%d %d\n",p,q);
     }
     return 0;
 }

 

 


  1. 网站做得很好看,内容也多,全。前段时间在博客园里看到有人说:网页的好坏看字体。觉得微软雅黑的字体很好看,然后现在这个网站也用的这个字体!nice!

  2. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!