简体   繁体   English

我不确定为什么要获得Codechef的TLE

[英]I am not sure why I am getting a TLE for codechef

When I submit my solution to codechef, I keep getting a time limit error. 当我将解决方案提交给Codechef时,我总是收到时间限制错误。 I switched to buffered reader from scanner, but that did not fix it. 我从扫描仪切换到缓冲读取器,但是并不能解决问题。 I'm thinking it is in my algorithm, but I am not sure where, other than checking each 5 decrement could be unnecessary. 我以为它在我的算法中,但是我不确定在哪里,除了检查每个5的减量可能没有必要。 Where is the issue I am having located so that I can figure out how to solve it? 我在哪里找到问题,以便找出解决方法?

Here is the link for reference: https://www.codechef.com/problems/FCTRL 这是参考链接: https : //www.codechef.com/problems/FCTRL

Here is my code: 这是我的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Factorial {
    public static void main(String[] args) throws IOException {
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

        //how many numbers follow
        int number = Integer.parseInt(keyboard.readLine());
        //stores zSum's to output later
        int[] answerArray = new int[number];

        for(int i = 0; i < number; i++) {
            //the number to compute factorial
            int n = Integer.parseInt(keyboard.readLine());

            // moves number to one ending in 5
            n -= n % 5;
            //stores number of zeros
            int zSum = 0;

            for (int j = n; j > 0; j -= 5) {

                //if a power of 5, add 1 to zSum
                for (int k = 5; k <= j; k *= 5) {
                    if (j % k == 0) {
                        zSum ++;
                    }
                }
            }
            answerArray[i] = zSum;
        }

        //println all values in array
        for (int i = 0; i < number; i++) {
            System.out.println(answerArray[i]);
        }
    }
}

Your complexity is to high, 您的复杂性很高,

for (int j = n; j > 0; j -= 5) 对于(int j = n; j> 0; j-= 5)

will do O(N) operations. 将执行O(N)运算。 Inner cycle will add log to complexity. 内部周期将增加日志的复杂性。 You should write use some other approach. 您应该使用其他方法编写。 It can be done using O(log(N)) time for each testcase. 可以使用每个测试用例的O(log(N))时间来完成。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在为 CodeChef 编写代码。 但是得到错误 TLE 是否可以对其进行更多优化? - I am writing a code for CodeChef. but getting error TLE is it possible to optimize it more? 我在Codechef中收到NZEC错误 - I am getting an NZEC error in codechef 为什么我在使用 ArrayList 时出现 Time Limit Exceeded (TLE) 错误<string[]>而不是 int[][]?</string[]> - Why am I getting Time Limit Exceeded (TLE) error when using ArrayList<String[]> instead of int[][]? 我不确定为什么A的对象最后也要打印 - I am not sure why object of A is also getting printed at the last 不知道为什么我得到java.lang.NullPointerException - Not sure why I am getting java.lang.NullPointerException 不知道为什么我会收到这个“找不到符号”的错误 - Not sure why I am getting this "cannot find symbol" error 不知道为什么我从httpurlconnection得到一个空的输出流 - not sure why I am getting an empty outputstream from httpurlconnection 为什么我在spring工具套件中遇到这些错误。 我认为这是因为我缺少一些库,但我不确定 - Why am I getting these errors in the spring tool suite. I think it is because I am missing some libraries but I am not sure 如果我以有效的方式找到一个大数字的阶乘,但我仍然得到 TLE,我该怎么办? - What should I do if I am finding the factorial of a big Number in an efficient way still I am getting a TLE? 不知道为什么我不遍历数组 - Not sure why I am not moving through array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM