Cod sursa(job #30370)

Utilizator victorsbVictor Rusu victorsb Data 13 martie 2007 20:33:22
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 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;
}