Skip to main content
added 171 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        int[] C = new int[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextInt();

        // ...
    }
}

All the input values fit within an int, so I changed the type of C to int[], and use .nextInt for all inputs. I left the type of A as long[] to let the A[i] * C[i] calculation step exceed the int limit (before the modulo).


Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Unfortunately none of this will get you pass all tests. I have a feeling that the fastest solution is more a matter of math and logic than code review material.


Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        int[] C = new int[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextInt();

        // ...
    }
}

All the input values fit within an int, so I changed the type of C to int[], and use .nextInt for all inputs. I left the type of A as long[] to let the A[i] * C[i] calculation step exceed the int limit (before the modulo).


Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        int[] C = new int[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextInt();

        // ...
    }
}

All the input values fit within an int, so I changed the type of C to int[], and use .nextInt for all inputs. I left the type of A as long[] to let the A[i] * C[i] calculation step exceed the int limit (before the modulo).


Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Unfortunately none of this will get you pass all tests. I have a feeling that the fastest solution is more a matter of math and logic than code review material.


Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

added 214 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        long[]int[] C = new long[M];int[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextLongnextInt();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextLongnextInt();

        // ...
    }
}

All the input values fit within an int, so I changed the type of C to int[], and use .nextInt for all inputs. I left the type of A as long[] to let the A[i] * C[i] calculation step exceed the int limit (before the modulo).


Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        long[] C = new long[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextLong();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextLong();

        // ...
    }
}

Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        int[] C = new int[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextInt();

        // ...
    }
}

All the input values fit within an int, so I changed the type of C to int[], and use .nextInt for all inputs. I left the type of A as long[] to let the A[i] * C[i] calculation step exceed the int limit (before the modulo).


Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

added 117 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        long[] C = new long[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextLong();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextLong();

        // ...
    }
}

Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        long[] C = new long[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextLong();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextLong();

        // ...
    }
}

Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Scanner is very ergonomic for parsing inputs, you can simplify your code quite a bit, instead of StringTokenizer (of which you created many):

class Solution {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        long[] A = new long[N];
        int[] B = new int[M];
        long[] C = new long[M];
        for (int i = 0; i < N; i++) A[i] = scanner.nextLong();
        for (int i = 0; i < M; i++) B[i] = scanner.nextInt();
        for (int i = 0; i < M; i++) C[i] = scanner.nextLong();

        // ...
    }
}

Another thing, BufferedReader closes the underlying stream, in this case System.in (see the implementation on grepcode). I think it's not a good practice to close a stream you didn't open, normally it should be the caller's responsibility.


How about giving the \$10^9 + 7\$ constant a name:

private static final int MODULO = (int) 1e9 + 7;

Btw, next time it's good if you include the full code. That way we can test run our suggestions easier on the hosting website, otherwise we have to guess that the implementation must be in the main method of a class named Solution.

Rollback to Revision 1
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading
added 195 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading