Chinmay Pani - 1 year ago 69

C Question

why is my code http://ideone.com/zm6hP7 not satisfying all test cases in the problem https://www.hackerrank.com/contests/opc-a/challenges/infinite-series ?

I have downloaded unsatisfied test cases and still can't find any problem in my code....can somebody tell me what the problem is???

`#include<stdio.h>``

#define m 1000000007

int main()

{

long long int t;

scanf("%lld",&t);

for(long long int i=0;i<t;i++)

{

long long int l,r;

scanf("%lld %lld",&l,&r);

long long int x,y;

if(l%2==0)

x=(((l/2)%m)*((l-1)%m))%m;

else

x=((l%m)*(((l-1)/2)%m))%m;

if(r%2==0)

y=(((r/2)%m)*((r+1)%m))%m;

else

y=((r%m)*(((r+1)/2)%m))%m;

printf("%lld\n",y-x);

}

return 0;

}

Answer Source

The likely mistake is that `y-x`

is negative when y(mod m) < x(mod m). This can be corrected by adding this at the end, before the `printf()`

statement:

```
if(y-x < 0)
{
y += m;
}
```