Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя Qualified

Автор Qualified, история, 4 года назад, По-английски

I am doing 1066A - Вова и поезд 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.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

"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.