zoukankan      html  css  js  c++  java
  • hdu 1239 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): 3993    Accepted Submission(s): 2097

    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
     
    Recommend

    Eddy

    题意:

    给你一个大于4的整数m和一个真分数a/b,求最佳素数对p、q,使得a/b<=p/q<=1且pq<=m。最佳即为满足条件的pair中pq最大的一对。

    感想:

    关于素数打表的各种方法还是要好好研究一下。。

     

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define N 100005
    using namespace std;
    
    bool no_prim[N];
    int m,a,b,p,q;
    
    void primetable()
    {
        int i,j;
        for(i=3;i<317;i+=2)
        {
            if(no_prim[i]==false)
            {
                int tmp=i<<1;
                for(j=i*i;j<N;j+=tmp)
                    no_prim[j]=true;
            }
        }
    }
    
    int main()
    {
        int i,j;
        primetable();
        while(scanf("%d%d%d",&m,&a,&b)&&m&&a&&b)
        {
            p=q=0;
            double limit=a*1.0/b;
            for(i=2;i<=m;i++)
            {
                if(!no_prim[i]&&i%2!=0||i==2)
                {
                    for(j=2;j<=i&&i*j<=m;j++)
                    {
                        if(!no_prim[j]&&j%2!=0||j==2)
                        {
                            if(j*1.0/i>=limit&&i*j>p*q)   //i和j的顺序
                            {
                                p=j;
                                q=i;
                            }
                        }
    
                    }
                }
            }
            printf("%d %d
    ",p,q);
        }
        return 0;
    }
    


     

  • 相关阅读:
    vim的modeline
    python的read() 、readline()、readlines()、xreadlines()
    hashset
    java泛型
    eclipse常用快捷键
    互联网计费模式
    cocos2d::CCFileUtils::sharedFileUtils()->getFileData(szFile, "r", &bufferSize) 不同平台返回值不一样
    CSS为英文和中文字体分别设置不同的字体
    fatal error C1010: 在查找预编译头时遇到意外的文件结尾
    JavaScript权威指南第03章 类型、值和变量(1)
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3230983.html
Copyright © 2011-2022 走看看