code fails due to large input

So the question is this https://www.hackerrank.com/challenges/summing-the-n-series

my code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>
#include<cmath>
using namespace std;
int main(int argc, char *argv[])
{
	long double div;
	//div=((1e+9)+7);
	int T;
	 long double n=0;
	 long  double sum=0;
//	cout<<div<<endl;
	cin>>T;

	for(int i=1;i<=T;i++)
	{
		n=0;
		//sum=0;
		cin>>n;
		
	//	sum=sum+n*n;
	//	cout<<sum<<endl;
	//	static_cast<long double>(sum);
	//	static_cast<long double>(div);
		long long int result=fmodl((n*n),((1e+9)+7));
		cout<<result<<endl;
		
	}
	return 0;
}


the problem is when the hackerrank inputs the following
10
5351871996120528
2248813659738258
2494359640703601
6044763399160734
3271269997212342
4276346434761561
2372239019637533
5624204919070546
9493965694520825
8629828692375133

My code gives the wrong output.

The output is supposed to be
578351320
404664464
20752136
999516029
743537718
323730244
174995256
593331567
136582381
305527433

So what changes should I make to get my code give the correct output.
Please help me.

what machine you are using, sometime math processor affect the output on big numbers.
I gave it a try but my attempt failed test cases 5 and 6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdint>

int main()
{
    int T = 0;
    std::cin >> T;
    std::uint64_t N = 0;

    while (std::cin >> N)
    {
        std::uint64_t   sum = 0,
                        T1 = 1,
                        TN = (2 * N) - 1;

        //std::cout << "TN = " << TN << std::endl;
        sum = ((N * (T1 + TN)) / 2) % 1000000007;

        std::cout << sum << std::endl;//578351320
    }
}


I'll post test case 6 in case it helps you or anyone else that wants to give it a try.
Input:

10
5773408242017850
5025554062339313
9803332417649065
4529826640896246
7633499047094366
4614556128541569
8200111660324493
9428242699249167
3888311265122983
2400440231598721


Output:

112242846
224225402
27866312
696985755
210094750
364229804
770629628
249617754
321706764
69640712
what machine you are using, sometime math processor affect the output on big numbers.


I'm using 32 bit P4 and my compiler is C free 5. But would it depend on my compiler? Shouldn't it depend on hackerrank's compiler?
I gave it a try but my attempt failed test cases 5 and 6


My code is also failing Test case 5 and 6.

Does anybody know why is it like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

constexpr unsigned long long MOD = 1000000007 ;

unsigned long long modular_sqare( unsigned long long n )
{
    n %= MOD ;

    static_assert( ( (MOD*MOD) / MOD ) == MOD, "integer overflow" ) ;
    // note: if this static assertion fails,
    // see: https://en.wikipedia.org/wiki/Modular_arithmetic#Example_implementations

    return ( n * n ) % MOD ;
}

int main()
{
    const unsigned long long test_data[] =
    {
        5351871996120528,
        2248813659738258,
        2494359640703601,
        6044763399160734,
        3271269997212342,
        4276346434761561,
        2372239019637533,
        5624204919070546,
        9493965694520825,
        8629828692375133
    };

    for( auto n : test_data ) std::cout << modular_sqare(n) << '\n' ;
}

http://coliru.stacked-crooked.com/a/761427751960e938
Thanks for the reply.

The code works but I don't understand several things.


1) Doesn't "unsigned long long n " require datatype like float or int?Why is the compiler accepting "unsigned long long n " without int or float?

2)What is "constexpr "?

3)What is "static_assert"?

4)Finally, what is " for( auto n : test_data )"? I've only used "for" for loops till now. I don't understand why we have " auto n : test_data" inside it.
) Doesn't "unsigned long long n " require datatype like float or int?Why is the compiler accepting "unsigned long long n " without int or float?


unsigned is the data type.

What is "constexpr "?
it is an expression the value of which can be determined at compile time, and is const.

What is "static_assert"?

static_assert creates a compiler error message when the expression is true (or false I forget)


Finally, what is " for( auto n : test_data )"? I've only used "for" for loops till now. I don't understand why we have " auto n : test_data" inside it.

This is a ranged for loop. Its just a shortcut for writing a for loop.
http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
Yanson wrote:
unsigned is the data type.

No. The data type is unsigned long long, though you could call it an unsigned long long.

@Gamemaster007: There are multiple versions of integral types. From smallest range to longest, you have char (technically an integral type), short, int, long, and long long. Each one is defined to be at least as large as the one before it. On my system, a char is 8 bits, a short is 16 bits, int and long are 32 bits, and long long is 64.

unsigned (as you may well know) just means that the number doesn't have a 'sign', i.e. all the values are positive. This means that since it doesn't have to store all the negative numbers, it can get numbers approximately twice as large. e.g. for char the range changes from -128 - 127; to 0 - 255.

Also, static_assert throws a compiler error if the parameter is false.
long long and unsigned long long are fundamental C++ integer data types. (In practice, these are 64-bit integer types.) http://en.cppreference.com/w/cpp/language/types


The constexpr in constexpr unsigned long long MOD = 1000000007 ; implies const and specifies that the value can be evaluated at compile-time. http://www.stroustrup.com/C++11FAQ.html#constexpr
In this particular case, the effect is the same as if we had written: const unsigned long long MOD = 1000000007 ;

static_assert is an assertion checked at compile-time. http://www.stroustrup.com/C++11FAQ.html#static_assert
static_assert( ( (MOD*MOD) / MOD ) == MOD, "integer overflow" ) ;
verifies at compile-time that MOD*MOD (1000000007*1000000007) does not cause an integer overflow.
If the assertion holds, the line has no effect; if it does not, there is a compile-time error.

for( auto n : test_data ) is a range-based for loop. http://www.stroustrup.com/C++11FAQ.html#for
http://www.stroustrup.com/C++11FAQ.html#auto
Last edited on
Topic archived. No new replies allowed.