Cod sursa(job #2417303)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 29 aprilie 2019 14:47:35
Problema Fractal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define ll long long
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int solve(int k, int x,int y){
  if(k == 1){
    //cout << ":" << x << " " << y << ":\n";
    if(x == 1 && y == 1)
      return 0;
    else if(x == 1 && y == 2)
      return 1;
    else if(x == 2 && y == 2)
      return 2;
    else if(x == 2 && y == 1)
      return 3;
    else {
      return 0;
    }

  } else {
    int dim = (1 << (k - 1)), k2 = k - 1;

    if(x <= (1 << k2) && y <= (1 << k2)) {
      return (dim * dim - 1) - solve(k - 1, dim + 1 - y, x);
    } else if(x <= (1 << k2) && (1 << k2) < y) {
      y -= (1 << k2);
      return (dim * dim) + solve(k - 1, x, y);
    } else if((1 << k2) < x && (1 << k2) < y) {
      x -= (1 << k2);
      y -= (1 << k2);
      return (dim * dim) * 2 + (dim * dim - 1) - solve(k - 1, dim + 1 - x, y);
    } else if((1 << k2) < x && y <= (1 << k2)){
      x -= (1 << k2);
      return (dim * dim) * 3 + solve(k - 1, dim + 1 - y, dim + 1 - x);
    } else {
      return 0;
    }
  }
}

int main()
{
  int k, x, y;
  in >> k >> x >> y;
  out << solve(k, x, y);
  return 0;
}