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

Автор shank_punk, история, 9 лет назад, По-английски

Hi guys. I tried implementing sieve today (I have done it 50+ times before), and my code was giving Runtime error :(

I tried to find out the error in my code but I don't know what is wrong.


#include<bits/stdc++.h> using namespace std; bool vis[1000002]; int main(){ int i,j; for(i=2;i<=100000;i++) vis[i]=true; for(i=2;i<=100000;i++){ if(!vis[i]) continue; for(j=i*i;j<=100000;j+=i){ vis[j]=false; } //cerr<<i<<" "; } return 0; }

If I change j+=i to j++, it does not show runtime error!(& the code will obviously be wrong & TLE)

UPDATE : Found my mistake(updated in comment section).

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

»
9 лет назад, # |
Rev. 3   Проголосовать: нравится +8 Проголосовать: не нравится

UPDATE : I found out the error.

Its j = i*i. Since I declared i,j as int, 100000*100000 will cause overflow(don't know the exact technical term to be used). So I changed int to long long & it worked :)