Qualified's blog

By Qualified, history, 5 years ago, In English

I am doing 1066A - Vova and Train and I am using Gvim. Here is my source code below.

Code

It always says 'terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc shell returned 3

Need help! Thanks in advance! Don't know why the font is weird for my code.

Looks like it works when I run it through the sample test cases. Submission says Memory Limit Exceeded.

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

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

Can you send the link of submitted code because I think there are lots of errors in above code.

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

    80174186 Thanks!

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

      firstly using ll=long long is not right you should write #define ll long long instead.

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

        I changed to #define ll long long but I am still getting Memory Limit exceeded. Here is the submission: 80174981

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Your solution uses too much memory. In this loop:

		for(ll i = 1; i <= d / v; i++) {
			a.pb(v * i);
		}

you add $$$d / v$$$ values to your vector, but in the sample already, we have $$$d = 10^9$$$ and $$$v = 1$$$, so $$$d / v = 10^9$$$ which is too much. try finding a more efficient solution.

Btw. using ll = long long is not incorrect, but I wouldn't consider it as nice. I agree with ronak037 that #define ll long long is much better

»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

"Memory limit exceeded" means you have used more memory than the given memory limit ($$$256$$$ megabytes). The reason being you are trying to store a lot of long long ints, $$$10^9$$$ in the worst case. Each long long int takes up 8 bytes. Hence, in the worse case, you are using $$$8*10^9$$$ bytes, or, $$$8000$$$ megabytes.

The exception bad_alloc is thrown when the program fails to allocate the requested memory space. You can read more about it here.

You can not use this much memory at a time. Try to think of a better approach.