So I'm trying to solve this problem but my solution has O(n^6) complexity and I don't know how you can do this faster.
int ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k = 0; k < n; k++){
for(int x = 0; x < n; x++){
for(int y = 0; y < n; y++){
for(int z = 0; z < n; z++){
if(i + j + k == x + y + z) ans++;
}
}
}
}
}
}
cout << ans << "\n";
I looked up the sequence on OEIS but there isn't any other relevant information.
https://oeis.org/search?q=1+%2C+20+%2C+141+%2C+580+%2C+1751&language=english&go=Search
I need to calculate this fast for n <= 100000.
Auto comment: topic has been updated by PoIycarp (previous revision, new revision, compare).
you can calculate number of tuples $$$0 \leq a, b, c \leq x$$$ where $$$a + b + c = x$$$ in $$$O(1)$$$, call this $$$w$$$, you can sum $$$w^2$$$ for all $$$1 \leq x \leq 3 \cdot n$$$
number of tuples can be calculated by $$$\binom{x + 2}{2}$$$ i think.
This is not true because in your solution, one of a, b, c may become greater than n-1 But this problem is solved with pie ( which one is greater than n-1) in O(1) Sorry if my English is not good
mb
with inclusion-exclusion:
in short, ans[sum] =
(+) (number of ways to choose 3 elements that sum up to "sum")
(-) (number of ways to choose 3 elements that sum up to "sum" but one of them is greater or equal to n) * (number of ways to choose that one element from the 3 chosen elements)
(+) (number of ways to choose 3 elements that sum up to "sum" but two of them are greater or equal to n) * (number of ways to choose those two elements from the 3 chosen elements)
correct me if i am wrong,
iterate on the sum (say $$$s$$$), we can as it'll be $$$\leq 3 n$$$
now we can calculate the number of solutions to $$$x + y + z = s$$$ using stars and bars, let that be equal to $$$c$$$
finally you choose two set of solutions and equate them so your answer will be $$$c * c$$$
bro the main part is finding c(which is doable using a diophantine equation) but you gonna calculate that for every answer from 0 to n-1 since you can't sum them up and raise it to the power of 2, why? because:
$$$(x + y)^2 \neq x^2 + y^2$$$ there is $$$2xy$$$ remaining which requires everything separately
even if we could do that, finding
c
is a bit annoying using c++ since it can't do easy combinatoricsyou what
There is a formula on Oeis:
$$$f(n) = \frac{n(11n^4+5n^2+4)}{20}$$$
can u link the page?
It's just the link in his blog:
link
Just read all lines. You will see the formula
Yeah thats why I linked it , but how can you write an actual algorithm to solve this? This task was given at the national olympiad in my country and a lot of people solved it, but somehow I can't think how you can solve this any other way, of course they didn't have access to OEIS.
You can just believe that the answer is polinomial, and interpolate it:)
ummm? they use paper and induction or recursive functions in combinatorics to get to a formula?
there is also this recursive function formula:
$$$f_n = 6f_{n-1} - 15f_{n-2} + 20f_{n-3} - 15f_{n-4} + 6f_{n-5} - f_{n-6}$$$
If anyone found out what it represents and the logic behind it please reply
Let $$$f = (1 + x + ... + x^{n - 1})$$$
If you find $$$f^3$$$ you will solve the problem because $$$f [ x^k ]$$$ = number of solutions $$$x + y + z = k$$$
So you need only multiply polinomials. You can do it in $$$O(n^2)$$$ naive, or using fft
Here's an elementary solution that doesn't use any generating functions or mystic formulas. Probably, this is what people in your national OI used.
Let $$$\mathrm{dp}[k][s]$$$ be the number of $$$k$$$-tuples of integers in $$$0 \ldots n - 1$$$ summing to $$$s$$$. That is:
It should be clear that
If $$$s - n + 1$$$ is negative, start summing from 0 instead. How to calculate this quickly? Use prefix sums:
Now the answer is $$$\sum_{s = 0}^{3n - 3} \mathrm{dp}[3][s]^2$$$.