Cod sursa(job #967093)

Utilizator toranagahVlad Badelita toranagah Data 27 iunie 2013 01:37:15
Problema Fractal Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <iostream>
using namespace std;

ifstream fin("fractal.in");
ofstream fout("fractal.out");

int K, X, Y;
int base[2][2] = {{0, 3}, {1, 2}};
int compute_fractal(int k, int x, int y);

int main() {
  fin >> K >> X >> Y;
  fout << compute_fractal(K, X, Y);
  return 0;
}

int compute_fractal(int k, int x, int y) {
  if (k == 1) {
    return base[x - 1][y - 1];
  }
  
  int l = (1 << (k - 1));
  if (x <= l && y <= l) {
    return compute_fractal(k - 1, y, x);
  }

  if (x <= l && y > l) {
    return compute_fractal(k - 1, x - l, y) + l * l;
  }

  if (x > l && y > l) {
    return compute_fractal(k - 1, x - l, y - l) + 2 * l * l;
  }
  
  return compute_fractal(k - 1, y - 1, x) + 3 * l * l;
}