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. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...
随机推荐
- Java并发编程里的volatile。Java内存模型核CPU内存架构的对应关系
CPU内存架构:https://www.jianshu.com/p/3d1eb589b48e Java内存模型:https://www.jianshu.com/p/27a9003c33f4 多线程下的 ...
- LinkedHashMap源码剖析
首先还是类似的,我们写一个简单的LinkedHashMap的程序: public class Test { public static void main(String[] args) { Map&l ...
- pymysql的使用心得(1)------小细节,注意!
最近一段时间开始使用MySQL,使用的是pymysql库. 其中遇到过一些小问题,值得记录一下,以便今后使用的时候注意到. 表格的建立,代码如下: cursor.execute("creat ...
- angular之interceptors拦截器
<!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UT ...
- vue-router路由知识补充
1.render函数 var app = new Vue({ el: '#app', router, render: h => h(App) //新添加的函数操作 }) 我们新加了render: ...
- OpenGL使用libPng读取png图片
#include<stdarg.h> #include<png.h> #include<glut.h> #include<math.h> #includ ...
- centos7 JDK1.8
安装之前先检查一下系统有没有自带open-jdk rpm -qa |grep java rpm -qa |grep jdk 卸载找出的已安装Java相关rpm文件:rpm -e --nodeps 重新 ...
- [转]DOM 中 Property 和 Attribute 的区别
angular的文档: https://angular.io/guide/template-syntax#property-binding https://blog.csdn.net/sunq1982 ...
- ntpdate设置
ntpdate设置 学习了:https://www.cnblogs.com/ibnode/p/3573302.html http://www.blogjava.net/spray/archive/20 ...
- (转)Unity3D占用内存太大的解决方法
自:http://www.cnblogs.com/88999660/archive/2013/03/15/2961663.html 最近网友通过网站搜索Unity3D在手机及其他平台下占用内存太大. ...