Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

Please help me with FCTRL and FCTRL2

$
0
0

Hello, First of all thanks for the initative, i am new on this website and enjoy the challenges so far. I have some issues with these 2 beginner problems : FCTRL and FCTRL2. I have browsed the Forum already but I did not find anything relevant that might help me. I mainly code in Java and I am not an expert, the issues I am having are : - Wrong answer for FCTRL. - Too much time for FCTRL2.

In both case it takes at lest 2 minutes for the tests to compute and to send me a response.

I don't really see what is wrong in my code as I get the good answers for the examples given in the problem wording and it is fast.

Here are my programs :

FCTRL :

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Scanner;

    public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter stdout = new PrintWriter(System.out);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            BigDecimal value = new BigDecimal(sc.nextInt());
            int trailingZeros = 0;
            while (value.divide(new BigDecimal("5")).compareTo(new BigDecimal("1")) == 1) {
                trailingZeros += value.divide(new BigDecimal("5")).intValue();
                value = value.divide(new BigDecimal("5"));
            }
            stdout.println(trailingZeros);
        }
        sc.close();
        stdout.flush();
        stdout.close();
    }
}

FCTRL2 :

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static BigInteger[] compute(int max){
        BigInteger[] nFact = new BigInteger[max];
        nFact[0] = new BigInteger("1");
        for (int i = 1; i < max; i++) {
            nFact[i] = nFact[i-1].multiply(new BigInteger(i+1+""));
        }
        return nFact;
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter stdout = new PrintWriter(System.out);
        int n = sc.nextInt();
        List<Integer> values = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) values.add(sc.nextInt());
        int max = Collections.max(values);
        BigInteger[] nFact = compute(max);
        for (int i = 0; i < n; i++) {
            stdout.println(nFact[values.get(i)-1]);
        }
        sc.close();
        stdout.flush();
        stdout.close();
    }
}

I really hope someone can help me ! Thanks !

EDIT : For FCTRL to work I had to change the condition

value.divide(new BigDecimal("5")).compareTo(new BigDecimal("1")) == 1

in the while loop by :

value.divide(new BigDecimal("5")).compareTo(new BigDecimal("1")) == 1 || value.divide(new BigDecimal("5")).compareTo(new BigDecimal("1")) == 0

Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>