you have an an integer n such that 1<=n<= 10^12, find the sum of all triagular numbers that are less than or equal n,
time limit: 1 second
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | Kevin114514 | 3659 |
| 3 | jiangly | 3640 |
| 4 | turmax | 3450 |
| 5 | tourist | 3382 |
| 6 | strapple | 3360 |
| 7 | Um_nik | 3354 |
| 8 | squareOf105 | 3348 |
| 9 | maroonrk | 3312 |
| 10 | idontwannadothis…e | 3191 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | nik_exists | 151 |
| 3 | maspy | 149 |
| 4 | AmShZ | 144 |
| 4 | Um_nik | 144 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | BledDest | 133 |
| 9 | maroonrk | 132 |
| 10 | Proof_by_QED | 131 |
you have an an integer n such that 1<=n<= 10^12, find the sum of all triagular numbers that are less than or equal n,
time limit: 1 second
| Name |
|---|



just binary search the index of the last triangular number <=n and use the hockey stick theorem
did not understand hockey stick theorem, could you explain it please? how does it relate to the problem?
sum of first i triangular numbers is the ith tetrahedral number
It can be solved in
O(sqrt(n)). Triangular numbers isT(k)=k(k+1)/2, just increment k from 1 whileT(k)<=nand get a sum ofT(k)If you have q queries, it can be solved in
O(q*log(n)+sqrt(n))with precompute prefix sums. Code?And you can use binary search by last
T(k),sum of T(k) from 1 to mism(m+1)(m+2)/6And you can solve in
O(1)by solving equationm * (m+1) / 2 <= n ~ m^2 + m - 2 * n = 0.Solution:
m = (sqrt(8*n+1)-1)/2yes please, I would like to see you approach
I like that O(1) query, another thing that could be done here instead of using formula sqrt(8 * n + 1) — 1) / 2; it can be subs. by the following two lines of codes, 1-x=floor(sqrt(2*n)); 2-if(((x+1)*(x))/2>n)--x; x here is the number of the first triangular numbers <= n, then what is left is to calculate the sum of the first x triangular numbers.
x=floor(sqrt(2*n));if(((x+1)*(x))/2>n)--x;What's going on here? It is attack from one user with 15 accounts?