题目

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

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

一、题目大意与分析

1974年11月16日星期六下午,通过波多黎各的阿雷西博射电望远镜,人类向地外智慧发出了一条信息。该消息由1679位组成,打算翻译成23*73像素的矩形图片。由于23和73都是素数,所以23×73是每个边都大于1像素的平移矩形图片的唯一可能大小。当然,并不能保证接收者会试图将信息翻译成一幅矩形图片。即使他们会这样做,他们也可能错误地将像素放入矩形中。阿雷西博消息的发送者很乐观。
我们正在计划一个类似的项目。你在项目中的任务是找到最合适的宽度和高度的翻译矩形图片。术语“最合适”的定义如下。给出了一个大于4的整数m。还给出了小于或等于1的正分数A/b。图片的面积不应大于m。翻译图片的宽度和高度都应为素数。宽高比不应小于a/b,也不应大于1。在这些限制条件下,你应该最大化图片的面积。
换句话说,你将得到一个整数m和一个分数a/b,它认为m>4和0<a/b<1。你应该找到一对素数p,q,使得pq<=m和a/b<=p/q<=1,而且乘积pq取这对素数中的最大值。你应该报告p和q为“最合适”的宽度和高度的翻译图片。

==题目大致意思是给你m,a,b三个数,要你找出一对最大的p和q,使得pq<=m和a/b<=p/q<=1。我是在做搜索题刷到的,一直再想搜索该怎么做最好,后来发现,依据题目所给范围来看(4 < m <= 100000 and 1 <= a <= b <= 1000)p或者q不可能会超过10000,给的范围小,所以这题只需要筛选出素数然后枚举即可
素数筛选点这

二、代码

代码如下(示例):

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 10005
using namespace std;
int prime[maxn],m,a,b,num=0;
int vis[maxn];
void Erathosthenes()//筛选素数
{int n=10000;memset(vis,0,sizeof(vis));for(int i=2; i<=n; i++)if(!vis[i]){prime[num++]=i;for(int j=i*i; j<=n; j+=i)vis[j]=i;}}
int main()
{double s;int i,j;int p,q,sum;Erathosthenes();//考虑到所求素数不可能大于10000,所以直接筛选素数while(scanf("%d%d%d",&m,&a,&b)==3){if(m==0&&a==0&&b==0)break;double s=(double)a/b;sum=0;for(i=num-1;i>=0;i--){for(j=i;j>=0;j--){if(prime[i]>m||prime[j]*prime[i]>m||((double)prime[j]/prime[i])<s)//不符合条件continue;if(prime[i]*prime[j]>sum)//要取最大{sum=prime[i]*prime[j];p=prime[i];q=prime[j];}}}printf("%d %d\n",q,p);}return 0;
}

HDU 1239 Calling Extraterrestrial Intelligence Again相关推荐

  1. 【HDOJ】1239 Calling Extraterrestrial Intelligence Again

    这题wa了很多词,题目本身很简单,把a/b搞反了,半天才检查出来. 1 #include <stdio.h> 2 #include <string.h> 3 #include ...

  2. Calling Extraterrestrial Intelligence Again

    Calling Extraterrestrial Intelligence Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 6 ...

  3. 413:Calling Extraterrestrial Intelligence Again(翻译 )

    来源:http://noi.openjudge.cn/ch0207/413/ Calling Extraterrestrial Intelligence Again 总时间限制: 1000ms 内存限 ...

  4. 08:Calling Extraterrestrial Intelligence AgainMOOC程序设计算法基础期末第八题

    问题表述: A message from humans to extraterrestrial intelligence was sent through the Arecibo radio tele ...

  5. 【HDU/POJ/ZOJ】Calling Extraterrestrial Intelligence Again (素数打表模板)

    http://poj.org/problem?id=1411  POJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=168 ...

  6. 【筛法求素数】HDU-1239 Calling Extraterrestrial Intelligence Again

    注解 1.首先用筛法求100000以内的素数,并找到其个数. 2.在所有找到的素数中,遍历找一个素数对,满足: (1)二者相乘小于等于m: (2)二者相乘是所有满足条件的素数对中的最大的: (3)二者 ...

  7. POJ 1411 Calling Extraterrestrial Intelligence Again G++ 易超时

    #include <iostream> #include <cstdio> using namespace std; //英语 抄博友程序 博友程序更快 易超时 int pri ...

  8. 大学英语(第六册)复习(原文及全文翻译)——Unit 3 - The Quest For Extraterrestrial Intelligence(搜寻外星人)

    Unit 3 - The Quest For Extraterrestrial Intelligence Are we humans alone in the universe? Or is ther ...

  9. CodeForces刷题C语言:What is for dinner?、Reconnaissance 2、Shell Game、Extra-terrestrial Intelligence、Extra

    记录洛谷刷题c语言QAQ 一.What is for dinner? 题面翻译 题面描述 鲨鱼有 n n n 颗牙齿,分别分布于 m m m 行上,第 i i i 颗牙齿有一个初始活力值 c i c_ ...

最新文章

  1. Delphi 能不能从Ring 3进入Ring 0
  2. create tablespace 与 heap_insert 函数
  3. 程序员们,您还想熬夜吗?
  4. 化验室计算机系统验证风险评估,计算机化系统验证风险评估报告.doc
  5. 书评:Just the Computer Essentials(Vista)
  6. .Net Core中使用ref和Spanamp;lt;Tamp;gt;提高程序性能
  7. Java高级工程师必看系列,已拿到offer
  8. 横河川仪压力变送器调零_YOKOGAWA/横河EJX110A差压变送器的性能误差和精度介绍...
  9. 读者写者问题详解 操作系统
  10. 【模板】BM + CH(线性递推式的求解,常系数齐次线性递推)
  11. AsyncTask源代码解析
  12. csv可以保存特殊字符_浅谈CSV注入漏洞
  13. Maya vray XYZ皮肤贴图材质节点连接
  14. 新版DAEMON Tools Lite打不开 bin 文件解决方法
  15. 云计算,大数据,人工智能本质和概念
  16. html5头部区域标签,HTML5语义标签(header、section、footer)
  17. antdesign——select搜索
  18. 18款口碑爆棚手机浏览器,被一网打尽了
  19. 哈尔滨工业大学计算机考研资料汇总
  20. 读书笔记:杨家成的英语学习之路(附带作者人生感悟)

热门文章

  1. 计算机的发展史英语作文,A History of Modern Computing-现代计算机历史 (英文原版)
  2. 天语W700 adb驱动解决
  3. 软考新生必看!高项备考经验分享
  4. 响应式织梦模板旅游公司类网站
  5. 今天向大家推荐一批大数据书籍,大家可以在业余的时候阅读,加深对大数据的了解
  6. 面经--前程无忧(前锦网络)
  7. 四大主流CA机构——国产占据其一
  8. [2008-05-26]我的梦
  9. 数据分析师招聘情况之python分析
  10. NFC framework introduce