pmtotheam's blog

By pmtotheam, history, 4 years ago, In English

Hi guys. So I am solving this problem below in JAVA but I keep getting TLE for few testcases. I think it is related to slow I/O and so I tried different method for fast I/O but it is not working. Is there something else I can do here?

Problem: https://cses.fi/problemset/task/1723/

My Submission: https://ideone.com/zbuoA5

Update

My New Submission: https://ideone.com/pW5N7u

After looking all the comments, I tried to come up with the above code but it still gives TLE for 2 TCs. (In my previous submission, I got TLE for 6 TCs).

Is there any way to solve this question in JAVA?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Look Here: https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/

As far as I know, #4 is the fastest IO in java.

»
4 years ago, # |
  Vote: I like it +8 Vote: I do not like it

I seriously doubt that in a problem with n <= 100, m <= n^2, you are getting I/O speed issues. One way to check is to read the input and print something wrong. If it's W/A, then your I/O is fine.

Note that you are running in time log_2(10^9)*100^3. 1s is also quite tight, so I would recommend you try to find some better optimizations. Or just use C++ lol

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Ah. Modulos are slow. You might want to remove the first modulo on line 109, as 1e9+1e18+21 is still safe inside a long. It might pass

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

if you're TLEing on the same test case over and over, it's probably not slow IO, however if you TLE on like test 6 the first time and pass test case 10 but then TLE test case 10 and pass test 6 the second time, then it's probably slow IO related. also modulos are slow af and this seems like a dp/modulo issue

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Petr's template looks simple and good: 62693856

(He used some plugin but the template code can be used without the plugin). It uses BufferedReader, StringTokenizer and PrintWriter like most Java templates. Also putting your solution in a static class seems beneficial because you don't have to prefix every variable and function with 'static'. On the SPOJ IO test the template runs in 0.75 seconds, while C++ with cin/cout runs in 0.33 seconds.