Cod sursa(job #3291279)

Utilizator alexcazacudanCazacu Alexandru Dan alexcazacudan Data 3 aprilie 2025 22:03:18
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>
#include <iostream>
using namespace std;

ifstream f("fractal.in");
ofstream g("fractal.out");

int fractal(int k, int x, int y) {
  if (k == 1) {
    if (x == 1 && y == 1) return 0;
    if (x == 1 && y == 2) return 1;
    if (x == 2 && y == 2) return 2;
    if (x == 2 && y == 1) return 3;
  }

  int half = 1 << (k - 1);
  int pow4 = (1 << ((k - 1) << 1));
  int lowPow4Min1 = pow4 - 1;
  if (x <= half && y <= half) {
    return lowPow4Min1 - fractal(k - 1, half - y + 1, x);
  }

  if (x <= half && y > half) {
    return pow4 + fractal(k - 1, x, y - half);
  }

  if (x > half && y > half) {
    return 2 * pow4 + fractal(k - 1, x - half, y - half);
  }

  if (x > half && y <= half) {
    return 3 * pow4 + lowPow4Min1 - fractal(k - 1, y, (half << 1) - x + 1);
  }
}

int main() {
  int k, x, y;
  f >> k >> x >> y;

  g << fractal(k, x, y);
  return 0;
}