I was solving a problem on spoj,SPOJ CARDS The problem is easy and i am getting correct output for small numbers,But it seems to spoj is not accepting due to integer overflow , Then what integer type should i use?
OR is there any other problem that they are not accepting it ?I also don't know the test cases in which it might be failing
some of the accepted solution of other guys that seem to use the same logic and there solution is accepted accepted sol
Test case:
2
3
7
output:
15
77
.
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
unsigned long long int sum1=0;
unsigned long long int sum2=0;
sum1=((n*(n-1))/2)%1000007;
// cout<<"sum1 is"<<sum1;
sum2=(n*n+n)%1000007;
cout<<(sum1+sum2)%1000007<<endl;
}
}
EDIT
The answer get accepted when i use unsigned long long n but max value of n was 1000 000 then it should also get accepted in int n because nmax is under range of int
(n*n+n)would be the best candidate for an overflow.sum1=((n*(n-1))/2)%1000007looks dangerous when using unsigned types, if the value fornthat is entered can be less than, or equal to, zero.