-
Notifications
You must be signed in to change notification settings - Fork 14
/
Calculate Sum.cpp
60 lines (44 loc) · 922 Bytes
/
Calculate Sum.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <cstdio>
const int MOD = 1e9 + 7;
long long power(long long x, long long power)
{
long long result = 1;
while(power)
{
if(power%2 == 1)
result = (result*x)%MOD;
x = (x*x)%MOD;
power = power >> 1;
}
return result;
}
long long inverse(long long x)
{
return power(x, MOD - 2);
}
long long cube_sum(long long n)
{
long long sum = ((n*(n + 1)%MOD)*inverse(2))%MOD;
return (sum*sum)%MOD;
}
long long square_sum(long long n)
{
return ( ( (n*(n + 1)%MOD )* ( (2*n + 1)%MOD )%MOD )*inverse(6) )%MOD;
}
void solve()
{
long long n;
scanf("%lld", &n);
n %= MOD;
long long answer = square_sum(n)*inverse(2) + cube_sum(n)*inverse(2);
answer %= MOD;
printf("%lld\n", answer);
}
int main()
{
int no_of_test_cases;
scanf("%d", &no_of_test_cases);
while(no_of_test_cases--)
solve();
return 0;
}