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. 帮助企业更有效地利用数据,提供经营决策支持.让决策管理者随时随地获取关键信息,基于数字决策,最终提高决策水平. 包括范围(层次由低到高):数据报 ...
随机推荐
- Android 面试题集 包含答案
作者:guoxiaoxing 链接: https://github.com/guoxiaoxing/android-interview 本文基于作者采用的MIT协议分发. 手画一下Android系统架 ...
- noise_process.c
#include <math.h>#include "otdr_const.h"#include "haar.h"#include "ot ...
- 让QT/Embedded支持国际化
让QT/Embedded支持国际化 环境配置: Qt/Embedded ,在主机和目标板上存放路径都为:/root/qt-embedded-free- Qt/X11 3.3 (主要用到其中的lupda ...
- 【BZOJ】【2127】happiness
网络流/最小割 Orz Hzwer. 这题他题解说的比较简略……我手画了个图才明白过来…… 嗯对于每个人选文or理的单独收益大家应该很好理解……连S->i 权值为选文的喜悦值,i->T权值 ...
- python中的归并排序
本来在博客上看到用python写的归并排序的程序,然后自己跟着他写了一下,结果发现是错的,不得不自己操作.而自己对python不是非常了解所以就变百度边写,最终在花了半个小时之后就写好了. def m ...
- C#中三种定时器对象的比较 【转】
https://www.cnblogs.com/zxtceq/p/5667281.html C#中三种定时器对象的比较 ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.W ...
- C# WCF 完整实例,winform 窗体作为 宿主
上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...
- 简单JavaScript语句实现搜索关键字高亮功能
高亮功能主要是指对页面中指定区域的指定文字进行高亮显示,也就是背景着色.一般在搜索结果页面会经常用到这个功能. 下面就为大家提供一种解决方案,用javascript实现. 首先在<head> ...
- MySQL bin-log与主从服务器
试验环境 Ubuntu ...
- html 中shadow DOM 的使用
什么是shadow DOM? An important aspect of web components is encapsulation — being able to keep the marku ...