Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

can someone please explain the solution to this problem?

$
0
0

Can someone please explain to me what the logic behind the solution to this problem is:

"A Simple Equation" - http://www.codechef.com/problems/EQUATION

I looked at a few solutions that produced the correct answer and I still cannot figure out why it works.

For example:

#include <stdio.h>

long long f(long long n)
{
    long long r;
    if (n<0)
        return 0;
    r = ((n + 1)*(n + 2)*(n + 3));
    return r / 6;
}
main()
{
    int t;
    long long n, a, b, c, ct;
    scanf("%d", &t);
    while (t--){

        scanf("%lld%lld%lld%lld", &n, &a, &b, &c);
        ct = f(n) - f(n - a - 1) - f(n - b - 1) - f(n - c - 1) + f(n - a - b - 2) + f(n - b - c - 2) + f(n - a - c - 2) - f(n - a - b - c - 3);
        printf("%lld\n", ct);

    }
    return 0;
}

http://www.codechef.com/viewsolution/340267


Viewing all articles
Browse latest Browse all 40121

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>