#CSES TIME PROBLEM ON SAME WITH JAVA

1 messages ยท Page 1 of 1 (latest)

tawny knoll
sand impBOT
#

<@&987246399047479336> please have a look, thanks.

sand impBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

vast pulsar
#

I'm sorry this is not helpful at all but I just want to let you know that you have written the most C-like Java program I have ever seen. ๐Ÿ˜‚

vast pulsar
#

I need to fix this for you. You've made my night. ๐Ÿ˜‚

tawny knoll
#

yayy, the same concept got ac in cpp, so well there should be a way to faster this code Thinkfused

vast pulsar
#
package com.example.application;

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    private static long MOD = 1000000007L;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int T = 1;

        while (T-- > 0) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
            long[] dp = new long[k + 1];
            dp[0] = 1;
            for(int i = 1; i <= k; i++) {
                for(int j = 0; j < n; j++) {
                    if(i < arr[j]) {
                        continue;
                    }
                    dp[i] += dp[i - arr[j]];
                    dp[i] %= MOD;
                }
            }
            System.out.println(dp[k]);
        }
    }
}
sand impBOT
# vast pulsar ```java package com.example.application; import java.util.*; import java.lang.*...

Detected code, here are some useful tools:

Formatted code
package com.example.application;

import java.util. * ;
import java.lang. * ;
import java.io. * ;

class Main {
  private static long MOD = 1000000007L;
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int T = 1;
    while (T-- > 0) {
      int n = scanner.nextInt();
      int k = scanner.nextInt();
      int [] arr = new int [n] ;
      for (int i = 0; i < n; i++) {
        arr[i]  = scanner.nextInt();
      }
      long [] dp = new long [k + 1] ;
      dp[0]  = 1;
      for (int i = 1; i <= k; i++) {
        for (int j = 0; j < n; j++) {
          if (i < arr[j] ) {
            continue ;
          }
          dp[i]  += dp[i - arr[j] ] ;
          dp[i]  %= MOD;
        }
      }
      System.out.println(dp[k] );
    }
  }
}
vast pulsar
#

I'm not sure it's untested.

tawny knoll
#

dont tell me that

tawny knoll
vast pulsar
#

But you don't need to create a reader yourself, there's built in objects that read from stdin in Java

cedar sageBOT
#
public final class Scanner
  implements Iterator<String>, Closeable

A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:

tawny knoll
#

so i prefer using reader

vast pulsar
#

Okay so if you want to micro-optimize then

#

Have you ever heard of branchless programming?

vast pulsar
tawny knoll
#

need to learn that, i guess if it helps me with the java time

vast pulsar
#

Depending on how big the input is too, you might be able to take advantage of multi-threading

#

As for your algorithm itself; beats me.

#

You might also be able to hack the memory limit by using off heap memory

#

although idk how fast it would be

tawny knoll
#

umm, read it but i dont think cses needs it, like CSES problem set is beginners educational problem sheet to be precise, ummm i guess i will do dynamic section with cpp only, but this is the first time i got tle in reader class, i am using from more than a year, why java is not fast bruhhh pepe_sad

vast pulsar
#

You could also probably get away with 1 loop for i and j, and just derive j from i with an equation

#

See the thing is, Java is not meant to be micro optimized like this. ๐Ÿ˜›

tawny knoll
#

i can use that but logically thats the same thing Thinkfused

tawny knoll
#

anyone got any idea? pls help

#

same continues, with more dp problems, well i am doing now with cpp, but this is concerning, i cant do it whole with only cpp, if rest have same prob

snow sage
#

Your task is to find all possible combinations within an array of numbers to make a target value

snow sage
#

Hmm

#

I'm gonna give you sometime, you tell me if it's correct because I'm gonna guess this AF

#
    public static void main(String args[]) {
        int n = 9;
        int[] arr = {2,3,5};

        int total = 0;
        for (int j : arr) {
            total += (j % n);
        }
        total = (total - arr.length) + 1;
        System.out.println(total);
    }
#

If it's wrong, sorry

tawny knoll
#

not tle, but giving wa

#

well, no need to sorry, i think cses should give more time for java

snow sage
tawny knoll
#

i mean, it's wrong

snow sage
#

ah okay

tawny knoll
#

well, i got the same problem today, and i observe that string is adding multiple times, i just sort it out, so i only add string minimum times, and it got accepted

#

i think i need to find a way to lower the times, just something in code which is taking too much time, hmmm