Cod sursa(job #2308112)

Utilizator cosceexcosceex cosceex Data 26 decembrie 2018 13:50:56
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <cstdio>

#define Kmax 16

int k, x, y, d[Kmax];

void citire()
{
    scanf("%d %d %d\n", &k, &x, &y);
}

void solve()
{
    int i, n, x1, y1;
    long long sol = 0;

    d[1] = 3;

    for (i = 2; i <= k; ++i)
        d[i] = 4 * d[i - 1] + 3;

    for (i = k; i >= 0; --i)
    {
        n = 1 << i;
        if (x <= n && y <= n)
        {
            x1 = y;
            y1 = x;
        }
        else if (x > n && y <= n)
        {
            sol += d[i] + 1;
            x -= n;
            x1 = x;
            y1 = y;
        }
        else if (x > n && y > n)
        {
            sol += 2 * (d[i] + 1);
            x -= n, y -= n;
            x1 = x;
            y1 = y;
        }
        else if (x <= n && y > n)
        {
            sol += 3 * (d[i] + 1);
            y -= n;
            x1 = n - y + 1;
            y1 = n - x + 1;
        }

        x = x1, y = y1;
    }
    printf("%lld\n", sol);
}

int main()
{
    freopen("fractal.in", "r", stdin);
    freopen("fractal.out", "w", stdout);
    citire();
    solve();
    return 0;
}