Cod sursa(job #3346679)

Utilizator radu4.andrei9@gmail.comChelu Radu Andrei [email protected] Data 14 martie 2026 22:52:06
Problema Fractal Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <stdio.h>
#include <stdlib.h>

struct punct {
    int x, y;
};
void swap( int *x, int *y ) {
    int cop = *x;
    *x = *y;
    *y = cop;
}
long long pas_fractal( int k, struct punct * punct ) {
    if ( k == 0 )
        return 0;
    long long lungime_latura = 1LL << k, arie = ( 1LL << ( k - 1 ) ) * ( 1LL << ( k - 1 ) );
    long long jumatate = 1LL << (k - 1);

    if ( punct -> x < ( 1LL << ( k - 1 ) ) && punct -> y < ( 1LL << ( k - 1 ) ) ) {
        swap( &punct -> x, &punct -> y);
        return pas_fractal( k - 1, punct );
    }
    else if ( punct -> x >= ( 1LL << ( k - 1 ) ) && punct -> y < ( 1LL << ( k - 1 ) ) ) {
        punct -> x = punct -> x - jumatate;
        return arie + pas_fractal(k - 1, punct);
    }
    else if ( punct -> x >= ( 1LL << ( k - 1 ) ) && punct -> y >= ( 1LL << ( k - 1 ) ) ) {
        punct -> x -= jumatate;
        punct -> y -= jumatate;
        return arie * 2 + pas_fractal( k - 1, punct );
    }
    else if ( punct -> x < ( 1LL << ( k - 1 ) ) && punct -> y >= ( 1LL << ( k - 1 ) ) ) {
        swap ( &punct -> x, &punct -> y );
        punct -> x = jumatate - 1 - ( punct -> x - jumatate);
        punct -> y = jumatate - 1 -punct -> y;
        return arie * 3 + pas_fractal( k - 1, punct );
    }
}

int main() {

    FILE *f = fopen("fractal.in", "r"),
    *g = fopen("fractal.out", "w");
    if ( f == NULL ) {
        printf("nu s a deschis fractal.in");
        return -1;
    }
    if ( g == NULL ) {
        printf("nu s a deshcis fractal.out");
        return -1;
    }
    int k, x, y;

    fscanf(f, "%d %d %d", &k, &x, &y);
    x--, y--;
    struct punct punct;
    punct.x = y, punct.y = x;
    fprintf(g, "%lld", pas_fractal(k, &punct));
    fclose(f), fclose(g);
    return 0;
}