luogu-P1464 Function 【记忆化搜索】

根据题意模拟,然后用一个mem存储已经计算过的值。

#include
using namespace std;
#define what(x) cout << #x << " is " << x << endl;
#define LL long long

bool visited[21][21][21];
LL mem[21][21][21];

LL fun (LL a, LL b, LL c)
{
    if (a <= 0 || b <= 0 || c <= 0) return 1;
    if (a > 20 || b > 20 || c > 20) return fun(20, 20, 20);
    if (visited[a][b][c]) return mem[a][b][c];
    visited[a][b][c] = true;
    if (a < b && b < c) mem[a][b][c] = fun(a, b, c-1) + fun(a, b-1, c-1) - fun(a, b-1, c);
    else mem[a][b][c] = fun(a-1, b, c) + fun(a-1, b-1, c) + fun(a-1, b, c-1) - fun(a-1, b-1, c-1);
    return mem[a][b][c];
}

int main()
{
    memset(visited, 0, sizeof(visited));
    memset(mem, 0, sizeof(mem));
    LL a, b, c;
    while (cin >> a >> b >> c) {
        if (a == -1 && b == -1 && c == -1) return 0;
        printf("w(%lld, %lld, %lld) = %lld\n", a, b, c, fun(a, b, c));
    }
    return 0;
}

你可能感兴趣的:(洛谷)