vivek_ghosh's blog

By vivek_ghosh, history, 6 years ago, In English

I submitted a solution to problem 1025B - Weakened Common Divisor .I'm getting Runtime Error "Exit code is -1073741819" for testcase 1.

Here's the submission link: 41911755

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by vivek_ghosh (previous revision, new revision, compare).

»
6 years ago, # |
  Vote: I like it +3 Vote: I do not like it

You erase a set iterator, that you still later use in a loop.

To prevent that, if you want to erase an element in a set, first increment the iterator pointing to that element, and then erase the iterator previous to it.

Also, when erasing an element, don't dereference the iterator.

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Just replace the loop with something like that:

while( it != f.end() )
    if( x % (*it) and y % (*it) )
    { 
        auto iu = it; 

        iu++, f.erase(it), it = iu; 
    }
    else
        it++;

This should fix the problem. Either f.erase(it) or f.erase(*it) can be used to erase the item.

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Thanks.

  • »
    »
    6 years ago, # ^ |
    Rev. 3   Vote: I like it +1 Vote: I do not like it

    With pleasure.

    Another alternative to write the same loop is

    for( auto iu = it; it != f.end(); it = iu )
    {
        iu++;
    
        if ( ( x % *it ) and ( y % *it ) )
            f.erase( it );
    }