Skip to content

Commit 9ca5721

Browse files
committed
SPOJ added summatory problems
1 parent 9c7409e commit 9ca5721

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This is copied from Internet but algorithm is strange
2+
3+
// This can be used for contests , since I can't remember this algorithm, I didnt use it
4+
5+
#include <cstdio>
6+
7+
typedef long long i64;
8+
9+
const int MOD = 1000000003;
10+
11+
inline i64 mod(i64 a) {
12+
return a < MOD ? a : a % MOD;
13+
}
14+
15+
int main() {
16+
int test, i;
17+
i64 sum, n, p[6], co[6] = {0, 4, 30, 50, 30, 6}, inv = 441666668;
18+
scanf("%d", &test);
19+
while(test--) {
20+
scanf("%lld", &n);
21+
for(i = 1, p[0] = 1; i <= 5; i++) p[i] = mod(n * p[i-1]);
22+
for(i = 1; i <= 5; i++) p[i] = mod(co[i] * p[i]);
23+
for(i = 1, sum = 0; i <= 5; i++) sum = mod(sum + p[i]);
24+
sum = mod(sum * inv);
25+
printf("%lld\n", sum);
26+
}
27+
return 0;
28+
}
29+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
f(n) is defined as: f(n) = 13+23+33+...+n3, so it is the sum of the cubes of all natural numbers up to n.
3+
4+
In this problem you are about to compute,
5+
6+
f(1) + f(2) + f(3) + ... + f(n)
7+
Input
8+
9+
The first line is an integer T(1 = T = 100,000), denoting the number of test cases. Then, T test cases follow.
10+
11+
For each test case, there is an integer n(1 = n = 1,000,000) written in one line.
12+
Output
13+
14+
For each test case output the result of the summatory function described above.
15+
16+
Since this number could be very large, output the answer modulo 1,000,000,003.
17+
Example
18+
19+
Input:
20+
3
21+
2
22+
10
23+
3
24+
25+
Output:
26+
10
27+
7942
28+
46
29+
30+
ALGORITHM :
31+
32+
1. Dynamically store all computed values in array
33+
2. Access the final value in O(1)
34+
35+
*/
36+
37+
38+
39+
#include <cstdio>
40+
41+
typedef long long ullong;
42+
43+
const int MOD = 1000000003;
44+
45+
inline ullong mod(ullong input) {
46+
return input < MOD ? input : input % MOD;
47+
}
48+
49+
int main() {
50+
int test, i;
51+
ullong sum=0, n ,array[1000001], temp;
52+
array[0]=0;
53+
scanf("%d", &test);
54+
55+
for(ullong i=1 ; i<1000001; ++i)
56+
{
57+
temp = mod(mod(i*i)*i);
58+
sum += temp;
59+
array[i] = mod(array[i-1]+sum);
60+
}
61+
62+
while(test--)
63+
{
64+
scanf("%lld", &n);
65+
printf("%lld\n",array[n]);
66+
}
67+
return 0;
68+
}
69+

0 commit comments

Comments
 (0)