0

I'm trying to solve a problem on france IOI about a card game (named "Reussite" on level 4) that requires outputting every odd number, however I keep getting (0xC0000005) code if my input is bigger than about 48000 and I can't seem to find the error. Help!

here's my code:

#include<bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int n;cin>>n;vector<int> arr(n+1);

    for (int i = 2; i < n+1; ++i) {
        int m = i;
        //error is in this while loop
        while (i*m<=n){arr[i*m]=1;m++;}
    }
    
   
    for (int i = 0; i < n+1; ++i) {
        if (arr[i]==0) printf("%d \n",i);
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Dec 29 '22 at 21:16
  • And please don't use so-called "competition" or "judge" sites as learning or teaching resources, because most of them are not. And even the ones that claim to be tend to have bad and sometimes even invalid code as examples. – Some programmer dude Dec 29 '22 at 21:17

1 Answers1

3

When the value n is at least 46341, its square no longer fits into a 32-bit signed integer, so the expression i*m results in an integer overflow.

You might want to cast the values to long long, like while ((long long)i*m<=n) or while (i*1LL*m<=n), or change your code otherwise.

Gassa
  • 8,546
  • 3
  • 29
  • 49