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. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...
随机推荐
- 使用jquery操作iframe
1. 内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id="mainiframe ...
- pat 1060 比较科学计数法
trick: 1.前导0 如:000001,000.000001 2.出现0时也要按照科学计数法输出 e.g. 4 00000.00000 0001 NO 0.0000*10^0 0.1*10^1 3 ...
- 倒计时 总结 Timer Handler CountDownTimer RxJava MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- (转)Unity3d UnityEditor编辑器定制和开发插件
在阅读本教程之前,你需要对Unity的操作流程有一些基础的认识,并且最好了解内置的GUI系统如何使用. 如何让编辑器运行你的代码 Unity3D可以通过事件触发来执行你的编辑器代码,但是我们需要一些编 ...
- Linux上安装Bugzilla4.4小记
因项目需要,我受命在一台Linux服务器上搭建一个Bugzilla,进过一天的调试,这项任务总算完成了.现在可以肯定的说,安装过程不复杂,基本就是解压,填参数,执行命令.Bugzilla要跑起来,本机 ...
- Windows 抓取本地环路包
1.cmd,ipconfig查看自己的ip地址. 2.执行命令:route add 10.36.65.89 mask 255.255.255.255 10.36.65.1 metric 1,其中10. ...
- 封装scrollView 循环滚动,tableViewCell(连载) mvc
封装 封装 封装 ... 封装的重要性太重要了 给大家在送点干货 从一个项目中抽取出来的.和大家一起分享 封装scrollView 循环滚动.tableViewCell(连载) 明天还会更新 tabl ...
- oracle client字符集设置 乱码问题
程序员经常要连接数据库 下面 就说一下 oracle数据库 客户端与服务器端 字符集一致性的问题 这可以解决中文乱码,其他字符乱码问题 主要是指在sqlplus中,其他类似toad/pls ...
- 如何用7-zip创建自解压文件,7Z软件如何使用
1 要创建自解压文件,一般都是双击直接解压到C盘的Program Files文件夹里面,或许还需要在桌面创建一个快捷方式之类的.但是一般的绿色软件除了复制到Program Files还需要运行一下绿化 ...
- 算法笔记_108:第四届蓝桥杯软件类省赛真题(JAVA软件开发本科A组)试题解答
目录 1 世纪末的星期 2 振兴中华 3 梅森素数 4 颠倒的价牌 5 三部排序 6 逆波兰表达式 7 错误票据 8 带分数 9 剪格子 10 大臣的旅费 前言:以下试题解答代码部分仅供参考,若有不 ...