Cod sursa(job #3242067)

Utilizator vralexRadu Vasilescu vralex Data 8 septembrie 2024 15:39:44
Problema Fractal Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <stdio.h>
#include <stdlib.h>

// Check if long long is needed

int fractal(int order, int x, int y) {
  /* If order = 0, there are no segments on the curve.
  In fact, the curve is just a point in this case.
  If order = 0, then x and y must be 1 for the function call to make sense.
  */
  if (order == 0) return 0;
  int pow2 = (1 << (order - 1));
  // Segments needed to traverse a quarter: 4^(order-1) - 1.
  // Recurrence is a_(n+1) = 4*a_n + 3.
  int quarter = (1 << (2 * (order - 1))) - 1;
  if (x <= pow2 && y <= pow2) return fractal(order - 1, y, x);
  if (x <= pow2 && y > pow2)
    return quarter + 1 + fractal(order - 1, x, y - pow2);
  if (x > pow2 && y > pow2)
    return 2 * quarter + 2 + fractal(order - 1, x - pow2, y - pow2);
  if (x > pow2 && y <= pow2)
    return 3 * quarter + 3 + fractal(order - 1, pow2 + 1 - y, 2 * pow2 + 1 - x);
}

int main() {
  FILE *fp_in, *fp_out;
  int k, x, y;
  fp_in = fopen("fractal.in", "r");
  fp_out = fopen("fractal.out", "w");
  fscanf(fp_in, "%d %d %d", &k, &x, &y);
  fprintf(fp_out, "%d", fractal(k, x, y));
  fclose(fp_in);
  fclose(fp_out);
  return 0;
}