Click here to Skip to main content
15,911,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was solving a problem on spoj,SPOJ.com - Problem CRDS[^]
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

One of the accepted solution of other guys that use the same logic and his solution is accepted SPOJ CRDS – Cards | Xop Tutorials[^]





Test case:

2
3
7
output:

15
77

What I have tried:

#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;

    }

}
Posted
Updated 9-Jan-19 17:41pm

0) I am unfamiliar with the type you're using - unsigned long long int.

1) The variables n, sum1, and sum2 should ALL be ulong values (or at least all the same integer type).

2) You say it fails when you use "large numbers". In what way does it "fail"?

3) Since you're doing math, I'd consider using decimal as your type.
 
Share this answer
 
v3
Comments
kavinderrana121 9-Jan-19 15:15pm    
It gets accepted when i used the unsigned long long n But why this happened ? as the max value of int was given 1 000 000 so it should also get accepted on int also?
#realJSOP 9-Jan-19 15:41pm    
Make all of your integer types the same (unsigned long). You also have not said WHAT the exception is. As a test, I made n an int, and left sum1 and sum1 as unsigned longs, and .Net threw an exception because n wan't an unsigned long.
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
C++
((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.
 
Share this answer
 
Quote:
Integer overflow in below program

Use the debugger to see exactly what is going on in your code and to see where the overflow occurs.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900