Reputation: 67
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);
}
}
Upvotes: 0
Views: 42
Reputation: 8846
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.
Upvotes: 3