SIGSEGV in problem http://www.codechef.com/problems/TMSLT

Guys, I am getting SIGSEGV, please help.

Since the strength can’t be greater than 1000000, I used a boolean array s[1000000] of this size, true means the strength is taken by a person, if two persons are there with same strength then they would be in different teams and cancel out the difference of strengths, so for every strength temp that comes during iteration, I have used s[temp]=~s[temp] . Finally, I just add and subtract alternatively for every s[i]==true. Giving me SIGSEGV… please help.

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

int main()
{
int t;
scanf("%d",&t);
    while(t--)
{
    long n,a,b,c,d,i,diff=0,last;
    scanf("%ld%ld%ld%ld%ld",&n,&a,&b,&c,&d);
    bool s[1000000]={false};
    s[d]=true;
    last=d;
    for(i=1;i<n;i++)
    {
        long temp=(last*(a*last+b)+c)%1000000;
        s[temp]=(~(s[temp]));
        last=temp;
    }
    i=0;
    while(i<1000000)
    {
        if(s[i])
        {
            diff+=i;
            i++;
            while(i<1000000 && s[i]==false)
                i++;
            if(i<1000000)
            {
                diff-=i;
                i++;
            }
        }
        else i++;
    }
    if(diff>=0) printf("%ld\n",diff);
    else printf("%ld\n",diff-2*diff);
}
return 0;

}

value of temp is negative… there is overflow in expression
check my submission using assert
http://www.codechef.com/viewsolution/5654401

long temp=(last*(a * last+b)+c)%1000000;

1 Like