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

Автор dthreatz, 11 лет назад, По-английски

I'm getting a "Can't compile file: Compiled file is too large [40555520 bytes], but maximal allowed size is 33554432 bytes [CompileRequest {id='program.cpp', description='', file='program.cpp', resources='', type='cpp.g++11'}]." error when submitting my solution for Facebook Hacker Cup Round 1 problem A. However, the memory limit for this problem in the Gym section is 1024 MB. So, what's happening here?

Not doing anything crazy in my code either:

#include <iostream>
#define N 10000000
using namespace std;

int arr[N + 1] = {1};
void sieve()
{
    for(long i = 2; i <= N; i++){
        if(arr[i] > 0)
            continue;
        arr[i]++;
        for(long j = 2; i*j <= N; j++){
            arr[i*j]++;
        }
    }
}

int main()
{
    int t;
    cin >> t;
    sieve();
    long a, b, k;
    for(int tt = 1; tt <= t; tt++){
        long long ans = 0;
        cin >> a >> b >> k;
        for(long i = a; i <= b; i++){
            if(arr[i] == k)
                ans++;
        }
        cout << "Case #" << tt << ": " << ans << endl;
    }
    return 0;
}
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

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

C++ compiler allocates memory for static arrays in the image.

So int arr[N + 1] = {1}; increases your *.exe size by 10000000x4 bytes, which is too large. Using vector will solve your problem since it allocates memory in runtime from a heap. Something like this:

vector<int> arr; // it can be a global variable (no memory allocation so far)
void sieve()
{
    arr.resize(N+1); // allocate memory from heap
...
  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    That does work, but I still think something is not working well with the judge since I'm guessing the memory limit refers to both static and dynamic memory.

    If you look at previous accepted solutions for the same problem in the Gym section, you'll see people allocating even more memory in global arrays, so it seems that the judge's constraints for those problems were changed at some point recently.