Cod sursa(job #612846)

Utilizator alex_mircescuAlex Mircescu alex_mircescu Data 11 septembrie 2011 01:50:43
Problema Fractal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <stdio.h>
#include <math.h>

long K, X, Y, rez, tot;

void go(long K, long X, long Y) {
	if (K == 0) {
		return;
	}
	long put = 1 << (K - 1);
	if (X <= put && put >= Y) go(K - 1, Y, X);
	if (X <= put && put < Y) {
		tot += 3 * put * put;
		go(K - 1, 2 * put - Y + 1, put - X + 1);
	}
	if (X > put && put >= Y) {
		tot += put * put;
		go(K - 1, X - put, Y);
	}
	if (X > put && put < Y) {
		tot += put * put * 2;
		go(K - 1, X - put, Y - put);
	}
}

int main() {
	freopen("fractal.in", "r", stdin);
	freopen("fractal.out", "w", stdout);
	scanf("%ld %ld %ld", &K, &X, &Y);
	go(K, Y, X);
	printf("%ld\n", tot);
	return 0;
}