Calling Extraterrestrial Intelligence Again

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

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
 
思路: 先打个素数表....由于涉及的数据较大,采用离线素数法......
 #include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100
#include<cstdlib>
using namespace std;
int prime[maxn+],rank=;
bool isprime[maxn+]; void Prim()
{
int i,j;
memset(isprime,true,sizeof(isprime)); isprime[]=isprime[]=false;
for(i=;i*i<=maxn;i++)
{
if(isprime[i])
{
prime[rank++]=i;
for(j=*i;j<=maxn;j+=i)
isprime[j]=false;
}
}
for(j=i;j<maxn;j++)
if(isprime[j])
prime[rank++]=j;
} int main()
{ Prim();
for(int i=;i<rank;i++)
printf("%d ",prime[i]);
puts("");
return ;
}

这样处理之后会出现,需要对所需数组,进行查找,线性表中最快的查找方法为 二叉搜索....

何为二叉搜索.....

代码如下:

 int maze[maxn+];
int two_find(int a[],int m,int n)
{
int left=,right=n,mid;
while(left<right)
{
mid=(left+right)/;
if(a[mid]==m)
break;
else
if(a[mid]<m)
left=mid+;
else right=mid-;
}
return mid;
}

剩下的就是对问题进行遍历了....由于求最大值,pq.....所以采取从最大处开始搜索......

代码:

     #include<stdio.h>
#include<string.h>
#define maxn 100000
#include<stdlib.h>
int prime[maxn+],step=;
bool bol[maxn+];
int two_find(int m)
{
int left=,right=step-;
int mid;
while(left<right)
{
mid=(left+right)/;
if(prime[mid]==m)
break;
else if(prime[mid]>m)
right=mid-;
else left=mid+;
}
return mid;
}
int main()
{
int m,a,b,i,j,k;
/*¿ìËÙËØÊý±í*/
memset(bol,true,sizeof(bol));
bol[]=bol[]=false;
prime[step++]=;
/*³ýȥżÊý*/
for(i=;i<=maxn;i+=)
bol[i]=false;
/*¿ªÊ¼*/
for(i=;i*i<=maxn;i++)
{
/*ÎªËØÊý£¬´æÈ룬³ýµôÆä±¶Êý*/
if(bol[i])
{
prime[step++]=i;
/*´ÓÈý±¶¿ªÊ¼*/
for(k=*i,j=i*i; j<=maxn;j+=k)
bol[j]=false;
}
}
/*´òÏÂÒ»°ëËØÊý*/
for( ; i<=maxn ; i++ )
if(bol[i])
prime[step++]=i; while(scanf("%d%d%d",&m,&a,&b),m+a+b)
{
double cal=(double)a/b;
int max=two_find(m),ans=,ansx,ansy;
for(i=max;i>=;i--)
{
for(j=i;j<=max;j++)
{
if(prime[i]*prime[j]>m||(double)prime[i]/prime[j]<cal)
break;
if(prime[i]<=m/prime[j]&&(double)prime[i]/prime[j]>=cal)
{
if(ans<prime[i]*prime[j])
{
ans=prime[i]*prime[j];
ansx=i;
ansy=j;
}
}
}
}
printf("%d %d\n",prime[ansx],prime[ansy]);
}
return ;
}

HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again的更多相关文章

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

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

  2. hdu 1239 Calling Extraterrestrial Intelligence Again (暴力枚举)

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

  3. 【noi 2.7_413】Calling Extraterrestrial Intelligence Again(算法效率--线性筛素数+二分+测时)

    题意:给3个数M,A,B,求两个质数P,Q.使其满足P*Q<=M且A/B<=P/Q<=1,并使P*Q最大.输入若干行以0,0,0结尾. 解法:先线性筛出素数表,再枚举出P,二分出对应 ...

  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. Calling Extraterrestrial Intelligence Again POJ 1411

    题目链接:http://poj.org/problem?id=1411 题目大意:找两个素数p,q满足a/b<=p/q<=1 且p*q<=m,求p*q最大的一组素数对. 第一次想的是 ...

  6. 【HDOJ】1239 Calling Extraterrestrial Intelligence Again

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

  7. 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753

    下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...

  8. Hihocoder 1329 平衡树·Splay(平衡树)

    Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...

  9. Business Intelligence (BI)

    BI, 全称Business Inteligence. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...

随机推荐

  1. jquery 判断元素是否存在于数组中

    要判断数组中是否包含某个元素,从原理来来说,就是遍历整个数组,然后判断是否相等 可以使用Jquery提供的方法: $.inArray("元素(字符串)",数组名称) 进行判断 ,当 ...

  2. 最小二乘法least square

    上研究生的时候接触的第一个Loss function就是least square.最近又研究了一下,做个总结吧. 定义看wiki就够了.公式如下 E(w)=12∑n=1N{y−xWT}2E(w)=12 ...

  3. Jquery Mobile实例--利用优酷JSON接口读取视频数据

    本文将介绍,如何利用JqueryMobile调用优酷API JSON接口显示视频数据. (1)注册用户接口. 首页,到 http://open.youku.com 注册一个账户,并通过验证.然后找到A ...

  4. GUI程序设计2

    8. 按钮(JButton)使用示例 例14. 按钮使用示例. package GUI; import java.awt.BorderLayout; import java.awt.Container ...

  5. XSS第四节,XSS攻击实例(一)

    在开始实例的讲解之前,先看一下XSS的危害情况,第一张图中说明和XSS相关的CVE漏洞有7417个(http://web.nvd.nist.gov/view/vuln/search-results?q ...

  6. 数学图形之SineSurface与粽子曲面

    SineSurface直译为正弦曲面.这有可能和你想象的正弦曲线不一样.如果把正弦曲线绕Y轴旋转,得到的该是正弦波曲面.这个曲面与上一节中的罗马曲面有些相似,那个是被捏过的正四面体,这个则是个被捏过正 ...

  7. Cesium应用篇:1快速搭建 【转】

    范例中所有范例可以在Github中搜索:ExamplesforCesium Cesium ['siːzɪəm]是一款开源的JavaScript开源库,开发者通过Cesium,实现无插件的创建三维球和二 ...

  8. Anagrams leetcode java

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  9. MongoDB学习笔记(六)--复制集+sharding分片 && 总结

    复制集+sharding分片                                                               背景 主机 IP 服务及端口 Server A ...

  10. zypper命令使用示例

    导读 Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zyppe ...