# 1161 - Soma de Fatoriais

## Descrição

{% embed url="<https://www.beecrowd.com.br/judge/pt/problems/view/1161>" %}

## Solução

Os números fatoriais são grandes demais para caber em variáveis inteiras de 32 bits, que só conseguem representar até o número 2^32 - 1 = 4.294.967.295 (que, não se preocupe, não vou escrever em extenso aqui). Com isso, precisamos estender a representação do número com um long long, acrescentando mais 32 bits para a representação para 2^64 - 1 = 18.446.744.073.709.551.615, que é um número maior que 20!, o que cabe no nosso rol de resultados (com o maior resultado dentro 20! vezes 2). Para JavaScript, use `BigInt()` em vez dos números normais.

Você pode calcular cada fatorial usando o método recursivo normalmente, mas memorizar o resultado de valores previamente calculados vai te economizar bastante tempo de execução, podendo inclusive optar por implementar a função e colocar o comando `Fatorial(20)` para calcular previamente todos os casos possíveis antes de receber qualquer entrada.

{% tabs %}
{% tab title="C99" %}

```c
#include <string.h>
#include <stdio.h>

long long int F[21];

long long int Fatorial(int n)
{
    if (F[n] == 0)
        F[n] = n * Fatorial(n - 1);
    return F[n];
}

int main()
{
    int M, N;

    memset(F, 0, sizeof(F));
    F[0] = 1;

    while (scanf("%d %d", &M, &N) != EOF)
    {
        printf("%lld\n", Fatorial(M) + Fatorial(N));
    }

    return 0;
}
```

{% endtab %}

{% tab title="C++17" %}

```cpp
#include <iostream>
#include <vector>

using namespace std;

vector<long long int> F;

long long int Fatorial(int n)
{
    if (F[n] == 0)
        F[n] = n * Fatorial(n - 1);
    return F[n];
}

int main()
{
    int M, N;

    F.assign(21, 0);
    F[0] = 1;

    while (cin >> M >> N)
    {
        cout << Fatorial(M) + Fatorial(N) << endl;
    }

    return 0;
}
```

{% endtab %}

{% tab title="JavaScript 12.18" %}

```javascript
let input = require("fs").readFileSync("/dev/stdin", "utf8");
let lines = input.trim().split("\n");

let F = Array(21);
F.fill(null);
F[0] = BigInt(1);

const Fatorial = (n) => {
    if (!F[n]) F[n] = BigInt(n) * Fatorial(n - 1);
    return F[n];
};

while (lines.length) {
    let [M, N] = lines.shift().trim().split(' ').map((x) => parseInt(x));

    console.log((Fatorial(M) + Fatorial(N)).toString());
}
```

{% endtab %}

{% tab title="Python 3.9" %}

```python
F = [0 for _ in range(21)]
F[0] = 1


def Fatorial(n):
    if(F[n] == 0):
        F[n] = n * Fatorial(n - 1)
    return F[n]


while True:
    try:
        M, N = [int(x) for x in input().strip().split(' ')]

        print(Fatorial(M) + Fatorial(N))
    except EOFError:
        break
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xtecna.gitbook.io/solucoes-da-beecrowd/matematica/1161-soma-de-fatoriais.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
