HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again
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
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 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.
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.
#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的更多相关文章
- poj 1411 Calling Extraterrestrial Intelligence Again(超时)
Calling Extraterrestrial Intelligence Again Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- hdu 1239 Calling Extraterrestrial Intelligence Again (暴力枚举)
Calling Extraterrestrial Intelligence Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 【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,二分出对应 ...
- 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 ...
- Calling Extraterrestrial Intelligence Again POJ 1411
题目链接:http://poj.org/problem?id=1411 题目大意:找两个素数p,q满足a/b<=p/q<=1 且p*q<=m,求p*q最大的一组素数对. 第一次想的是 ...
- 【HDOJ】1239 Calling Extraterrestrial Intelligence Again
这题wa了很多词,题目本身很简单,把a/b搞反了,半天才检查出来. #include <stdio.h> #include <string.h> #include <ma ...
- 穷举(四):POJ上的两道穷举例题POJ 1411和POJ 1753
下面给出两道POJ上的问题,看如何用穷举法解决. [例9]Calling Extraterrestrial Intelligence Again(POJ 1411) Description A mes ...
- Hihocoder 1329 平衡树·Splay(平衡树)
Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...
- Business Intelligence (BI)
BI, 全称Business Inteligence. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...
随机推荐
- 【视频分享】Liger UI实战集智建筑project管理系统配商业代码(打印报表、角色式权限管理)
QQ 2059055336 课程讲师:集思博智 课程分类:.net 适合人群:中级 课时数量:23课时 用到技术:Liger UI框架.AJAX.JSON数据格式的序列化与反序列化.角色的交叉权限管理 ...
- javascript和python取dict字典对象的不同
dict1={"a":1,"b":2,"22a":44} JS: dict1.a 和 dict1["a"]都可以 pyt ...
- [转]Unicode和UTF-8的关系
Unicode和UTF-8的关系作者: 张军 原文地址: http://blog.renren.com/blog/284133452/485453790 今天中午,我突然想搞清楚Unicode和UTF ...
- 23.读写锁ReadWriteLock
ReentrantReadWriteLock 所谓的读写锁,是访问资源共享共享锁.互斥锁,如果对资源加了写锁,其他线程无法获取写锁与读锁,但是持有写锁的线程,可以对资源 加读锁:如果一 ...
- C#线程同步与死锁Monitor
在上一讲介绍了使用lock来实现C#线程同步.实际上,这个lock是C#的一个障眼法,在C#编译器编译lock语句时,将其编译成了调用Monitor类.先看看下面的C#源代码: public stat ...
- IIS6.0支持PHP设置
找到了一份兼职,做网站的,但是公司里面服务器是用的IIS,Win2003系统,而且以前的网站都是aspx的.老板是我们学校的一个老师,我是被学长推荐过去了. --------------------- ...
- DateNavigator
<Border BorderThickness="1,1,1,1" BorderBrush="Black" Grid.Column="1&quo ...
- idea 本地调用zookeeper配置
- Access2013 Mssql2012 数据库连接字符串
直接在调用的地方设置 // access 2003 DBConStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Ap ...
- JS计算本周一和本周五的日期
代码不长: var today=new Date();var weekday=today.getDay(); var monday=new Date(1000*60*60*24*(1-weekd ...