#include <stdio.h>
int process(int n, int *count) {
static int cache[100] = {0};
(*count)++;
if (n <= 1) return n;
if (cache[n] != 0) return cache[n];
int result;
if (n % 2 == 0) {
result = process(n / 2, count) + process(n / 2 - 1, count);
} else {
result = process(n - 1, count) - process(n - 2, count);
}
cache[n] = result;
return result;
}
int main() {
int count = 0;
int result = process(12, &count);
printf("%d %d\n", result
, count
);
count = 0;
result = process(12, &count);
printf("%d %d\n", result
, count
); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgcHJvY2VzcyhpbnQgbiwgaW50ICpjb3VudCkgewogICAgc3RhdGljIGludCBjYWNoZVsxMDBdID0gezB9OwogICAgKCpjb3VudCkrKzsKICAgIAogICAgaWYgKG4gPD0gMSkgcmV0dXJuIG47CiAgICBpZiAoY2FjaGVbbl0gIT0gMCkgcmV0dXJuIGNhY2hlW25dOwogICAgCiAgICBpbnQgcmVzdWx0OwogICAgaWYgKG4gJSAyID09IDApIHsKICAgICAgICByZXN1bHQgPSBwcm9jZXNzKG4gLyAyLCBjb3VudCkgKyBwcm9jZXNzKG4gLyAyIC0gMSwgY291bnQpOwogICAgfSBlbHNlIHsKICAgICAgICByZXN1bHQgPSBwcm9jZXNzKG4gLSAxLCBjb3VudCkgLSBwcm9jZXNzKG4gLSAyLCBjb3VudCk7CiAgICB9CiAgICAKICAgIGNhY2hlW25dID0gcmVzdWx0OwogICAgcmV0dXJuIHJlc3VsdDsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgY291bnQgPSAwOwogICAgaW50IHJlc3VsdCA9IHByb2Nlc3MoMTIsICZjb3VudCk7CiAgICBwcmludGYoIiVkICVkXG4iLCByZXN1bHQsIGNvdW50KTsKICAgIAogICAgY291bnQgPSAwOwogICAgcmVzdWx0ID0gcHJvY2VzcygxMiwgJmNvdW50KTsKICAgIHByaW50ZigiJWQgJWRcbiIsIHJlc3VsdCwgY291bnQpOwogICAgcmV0dXJuIDA7Cn0=