HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again

摘要:
调用外星智能Again时间限制:2000/1000MS(Java/其他)内存限制:65536/32768K(Java/其它)总提交数:4083接受的提交数:2140问题描述来自humanstoex的消息
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
Asia 2002, Kanazawa (Japan)
 
Recommend
Eddy
 
思路: 先打个素数表....由于涉及的数据较大,采用离线素数法......
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define maxn 100
 5 #include<cstdlib>
 6 using namespace std;
 7 int prime[maxn+2],rank=0;
 8 bool isprime[maxn+2];
 9 
10 void Prim()
11 {
12     int i,j;
13     memset(isprime,true,sizeof(isprime));
14     
15     isprime[0]=isprime[1]=false;
16     for(i=0;i*i<=maxn;i++)
17     {
18         if(isprime[i])
19         {
20             prime[rank++]=i;
21             for(j=2*i;j<=maxn;j+=i)
22                 isprime[j]=false;
23         }
24     }
25     for(j=i;j<maxn;j++)
26         if(isprime[j])
27             prime[rank++]=j;
28 }
29 
30 int main()
31 {
32 
33     Prim();
34     for(int i=0;i<rank;i++)
35         printf("%d ",prime[i]);
36     puts("");
37     return 0;
38 }

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

何为二叉搜索.....

代码如下:

 1 int maze[maxn+1];
 2  int two_find(int a[],int m,int n)
 3  {
 4    int left=0,right=n,mid;
 5    while(left<right)
 6    {
 7        mid=(left+right)/2;
 8        if(a[mid]==m)
 9            break;
10        else
11            if(a[mid]<m)
12              left=mid+1;
13            else right=mid-1;
14    }
15    return mid;
16  }

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

代码:

 1     #include<stdio.h>
 2     #include<string.h>
 3     #define maxn 100000
 4     #include<stdlib.h>
 5     int prime[maxn+2],step=0;
 6     bool bol[maxn+5];
 7     int two_find(int m)
 8     {
 9        int left=0,right=step-1;
10        int mid;
11        while(left<right)
12        {
13           mid=(left+right)/2;
14          if(prime[mid]==m)
15                 break;
16          else if(prime[mid]>m)
17              right=mid-1;
18          else left=mid+1;
19        }
20        return mid;
21     }
22     int main()
23     {
24         int m,a,b,i,j,k;
25       /*¿ìËÙËØÊý±í*/
26       memset(bol,true,sizeof(bol));
27       bol[0]=bol[1]=false;
28       prime[step++]=2;
29       /*³ýȥżÊý*/
30         for(i=4;i<=maxn;i+=2)
31             bol[i]=false;
32         /*¿ªÊ¼*/
33         for(i=3;i*i<=maxn;i++)
34         {
35             /*ΪËØÊý£¬´æÈ룬³ýµôÆ䱶Êý*/
36             if(bol[i])
37             {
38              prime[step++]=i;
39              /*´ÓÈý±¶¿ªÊ¼*/
40              for(k=2*i,j=i*i; j<=maxn;j+=k)
41                  bol[j]=false;
42             }
43         }
44         /*´òÏÂÒ»°ëËØÊý*/
45         for( ; i<=maxn ; i++ )
46             if(bol[i])
47                 prime[step++]=i;
48 
49         while(scanf("%d%d%d",&m,&a,&b),m+a+b)
50         {
51           double cal=(double)a/b;
52           int max=two_find(m),ans=0,ansx,ansy;
53           for(i=max;i>=0;i--)
54           {
55               for(j=i;j<=max;j++)
56               {
57                 if(prime[i]*prime[j]>m||(double)prime[i]/prime[j]<cal) 
58                        break;
59                 if(prime[i]<=m/prime[j]&&(double)prime[i]/prime[j]>=cal)
60                 {
61                     if(ans<prime[i]*prime[j])
62                     {
63                         ans=prime[i]*prime[j];
64                         ansx=i;
65                         ansy=j;
66                     }
67                 }
68               }
69           }
70           printf("%d %d
",prime[ansx],prime[ansy]);
71         }
72          return 0;
73     }
 

免责声明:文章转载自《HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ansible-主机分组如何将一个Maven项目转化成一个Eclipse项目下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

怎样将shp文件的坐标点导出来?

单击以选择保存类型中的文本文件,将经度和纬度输出为txt格式。坐标系统有两个选项。第一个是数据源的坐标系。数据的数据源坐标系为UTM,投影坐标系,单位为米。第二个是我开始设置的数据帧的坐标系,即WGS84,单位为度。。。。直接将获得的点的坐标生成到文本文件中。如果它是栅格文件,则来自rastrastertopint的arctoolboxconverttool...

IntelliJ IDEA(2017)安装和破解

IDEA全称IntelliJIDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、各类版本工具、JUnit、CVS整合、代码分析、创新的GUI设计等方面的功能可以说是超常的。用记事本打开分别在最下面一行增加一行:-javaagent:D:IDEAinJetbra...

建行手机银行4.0版本转账怎么不要求输入支付密码?

建行手机银行单笔限额50万,日限额100万,这个6位数的验证码价值50万元!输入6个数字的支付密码只需要几秒钟而已,转账操作频率不是很高,手机银行转账速度比人工柜台、ATM、电脑网银转账速度不知道快了多少倍,输入6个数字的支付密码这几秒钟相对安全性算什么呢?另外建行还有帐号支付的方式,对电子商户日限额10000元,只需要帐号+手机验证码就可以支付,密码都不用...

avue 常用修改

1.搜索栅栏调整colum中对象的属性:searchSpan:4,column:[{label:"模型名称",prop:"name",search:true,searchSpan:4,},2.搜索框字段位置长度column:[{label:"流程标题23423432",searchLabelWidth:200,3.编辑页面,字段lable宽度设置labelW...

ClickHouse之访问权限控制

Ck当前只有select和insert。这是我刚才提到的:60cd41aedc4e47e8883682b416109e7b7e345e15ecc63c2c98ecdab5e8e053a只读defaultdefault此部分意味着添加具有只读权限的dba用户。允许访问的数据库是默认值。源IP不受限制::/0尝试以dba用户身份登录:clickhouse-cli...

[Oracle]

MyOralceLinux6.5isrunningonVirtualBox.Basicsettingsis4Gmemory,50Ghard-disk,autopartitionwheninstallOL6.5.**Followingseriesofoperationsneedrootprivilege.1.Edit/etc/hostsAddyourhostn...