Codechef的答案错误。但是我的数学是正确的

时间:2019-03-18 10:46:25

标签: java

问题链接-https://www.codechef.com/problems/MATPH 因此,我在这个问题上停留了几个小时,而且我不知道自己错在哪里。 我使用Eratosthenes的Sieve来查找素数,并将所有素数保存在哈希图中。在线法官给我关于测试用例的错误答案。

        static void dri(int n) {
            long large=0;int r=0,x,count=0,p,count1=0;
            x=(int)Math.sqrt(n);

            //To understand why I calculated x let's take an example
            //let n=530 sqrt(530) is 23 so for all the numbers greater than 23 when 
            //we square them they will come out to be greater than n 
            //so now I just have to check the numbers till x because numbers 
            //greater than x will defiantly fail.I think you get 
            //what I'm trying to explain


            while(r<x) {
                r = map.get(++count); // Prime numbers will be fetched from map and stored in r
                int exp = (int) (Math.log(n) / Math.log(r));
                //To explain this line let n=64 and r=3.Now, exp will be equal to 3
                //This result implies that  for r=3 the 3^exp is the //maximum(less than n) value  which I can calculate by having a prime in a power
                if (exp != 1) {   //This is just to resolve an error dont mind this line
                    if (map.containsValue(exp) == false) {
                    //This line implies that when exp is not prime  
                    //So as I need prime number  next lines of code will calculate the nearest prime to exp
                        count1 = exp;
                        while (!map.containsValue(--count1)) ;  

                        exp = count1;
                    }
                    int temp = (int) Math.pow(r, exp);
                    if (large < temp)
                        large = temp;
                }
            }
            System.out.println(large);
        }

1 个答案:

答案 0 :(得分:0)

  

对于每个测试用例,在一行中包含最大的输出   美丽数字≤ N 。如果没有这样的数字,则打印-1。

我相信4是最小的美丽数字,因为2是最小的质数,并且2 ^ 2等于4。 N 仅需≥0。所以dri(0),{{ 1}},dri(1)dri(2)应该全部打印dri(3)。我试过了。他们没有。我相信这就是您在CodeChef上失败的原因。

我将自己留给自己,以了解所提及的对您的方法的调用的行为以及处理方法。

顺便说一句,将质数保存在地图中有什么意义?列表或排序集会更合适吗?