Your code is using the right datatype on the wrong side of the assignments. For instance in
Quote:
sum1=((n*(n-1))/2)%1000007;
sum1
can be a (32-bit)
int
since its computed values is always is less than
1000007
.
On the other hand,
((n*(n-1))/2)%1000007
must be written instead
((unsigned long long)n*(n-1))/2)%1000007
in order to force the correct evaluation, i.e. without overflow of
n*(n-1)
. Explicit cast is necessary (you may use C++
static_cast
, if you like) because otherwise the whole expression is
first evaluated using
int
operands and
then the (overflown) result is promoted to
unsigned long long
.