Calling Extraterrestrial Intelligence Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3993    Accepted Submission(s): 2097

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
 
Source
 
Recommend

Eddy

题意:

给你一个大于4的整数m和一个真分数a/b,求最佳素数对p、q,使得a/b<=p/q<=1且pq<=m。最佳即为满足条件的pair中pq最大的一对。

感想:

关于素数打表的各种方法还是要好好研究一下。。

代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #define N 100005
  5. using namespace std;
  6.  
  7. bool no_prim[N];
  8. int m,a,b,p,q;
  9.  
  10. void primetable()
  11. {
  12. int i,j;
  13. for(i=3;i<317;i+=2)
  14. {
  15. if(no_prim[i]==false)
  16. {
  17. int tmp=i<<1;
  18. for(j=i*i;j<N;j+=tmp)
  19. no_prim[j]=true;
  20. }
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. int i,j;
  27. primetable();
  28. while(scanf("%d%d%d",&m,&a,&b)&&m&&a&&b)
  29. {
  30. p=q=0;
  31. double limit=a*1.0/b;
  32. for(i=2;i<=m;i++)
  33. {
  34. if(!no_prim[i]&&i%2!=0||i==2)
  35. {
  36. for(j=2;j<=i&&i*j<=m;j++)
  37. {
  38. if(!no_prim[j]&&j%2!=0||j==2)
  39. {
  40. if(j*1.0/i>=limit&&i*j>p*q) //i和j的顺序
  41. {
  42. p=j;
  43. q=i;
  44. }
  45. }
  46.  
  47. }
  48. }
  49. }
  50. printf("%d %d\n",p,q);
  51. }
  52. return 0;
  53. }

hdu 1239 Calling Extraterrestrial Intelligence Again (暴力枚举)的更多相关文章

  1. 【HDOJ】1239 Calling Extraterrestrial Intelligence Again

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

  2. poj 1411 Calling Extraterrestrial Intelligence Again(超时)

    Calling Extraterrestrial Intelligence Again Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  3. HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again

    Calling Extraterrestrial Intelligence Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  4. poj 1411 Calling Extraterrestrial Intelligence Again

    题意:给你数m,a,b,假设有数p,q,满足p*q<=m同时a/b<=p/q<=1,求当p*q最大的p和q的值 方法:暴力枚举 -_-|| and 优化范围 我们可以注意到在某一个m ...

  5. HDU - 1248 寒冰王座 数学or暴力枚举

    思路: 1.暴力枚举每种面值的张数,将可以花光的钱记录下来.每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的.时间复杂度O(n) 2.350 = 200 + 150,买350的 ...

  6. hdu 4082 Hou Yi's secret(暴力枚举)

    Hou Yi's secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 5944 Fxx and string(暴力/枚举)

    传送门 Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Othe ...

  8. hdu 4968 Improving the GPA (水 暴力枚举)

    题目链接 题意:给平均成绩和科目数,求可能的最大学分和最小学分. 分析: 枚举一下,可以达到复杂度可以达到10^4,我下面的代码是10^5,可以把最后一个循环撤掉. 刚开始以为枚举档次的话是5^10, ...

  9. HDU 5660 jrMz and angles (暴力枚举)

    jrMz and angles 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/E Description jrMz has tw ...

随机推荐

  1. HBase加Solr

    HBase加Solr 如何解决分布式系统数据事务一致性问题 (HBase加Solr) 摘要:对于所有的分布式系统,我想事务一致性问题是极其非常重要的问题,因为它直接影响到系统的可用性.本文以下所述所要 ...

  2. BroadcastReceiver.PendingResult类别

    java.lang.Object android.content.BroadcastReceiver.PendingResul 类概述 状态的结果正在等待一个广播接收器.在BroadcastRecei ...

  3. opengl微开发之1-从零開始

    对OpenGL有一点了解之后,如今開始真正编写代码. 今天的内容: 使用FreeGLUT创建OpenGL的上下文环境 初始化GLEW 创建一个OpenGL的的模板范例 第一步: 一个OpenGL的上下 ...

  4. 华为-on练习--身高找到最好的二人

    称号: 离5个人选择2个人作为礼工具.中的每个个体的身高的范围160-190,要求2个人高差值至少(假设差异值一样,他们中最高的选择).输出的两个人的身高升序. Smple input:161 189 ...

  5. Web API 2 对 CORS 的支持

    Web API 2 对 CORS 的支持 CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览 ...

  6. likely()与unlikely()

    he gcc C compiler has a built-in directive that optimizes conditional branches as either very likely ...

  7. Impala源代码分析---1

    2.Impala源代码分析 參考链接:http://www.sizeofvoid.net/wp-content/uploads/ImpalaIntroduction2.pdf 本章開始进入源代码分析阶 ...

  8. poj 2513 Colored Sticks(欧拉路径+并检查集合+特里)

    题目链接:poj 2513 Colored Sticks 题目大意:有N个木棍,每根木棍两端被涂上颜色.如今给定每一个木棍两端的颜色.不同木棍之间拼接须要颜色同样的 端才干够.问最后是否能将N个木棍拼 ...

  9. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  10. IOS开发中怎样验证邮箱的合法性

    IOS开发中怎样验证邮箱的合法性 文章参考:http://www.codes51.com/article/detail_94157.html 代码: - (void)viewDidLoad { [su ...