Skip to content

Instantly share code, notes, and snippets.

@ranha
Created May 26, 2012 13:11
Show Gist options
  • Save ranha/2793885 to your computer and use it in GitHub Desktop.
Save ranha/2793885 to your computer and use it in GitHub Desktop.
hoge
#include <stdio.h>
#include <setjmp.h>
#define HEAP 5000
const int N = 10;
enum Tag {
ask_depth,
answer
};
struct tuple {
int fst;
enum Tag snd;
};
jmp_buf env;
int i,tag;
int* heap[HEAP];
int **top;
#define timetravel(v) ({ \
if((tag = setjmp(env))==0){ \
for(i=0 ; i<HEAP; ++i)heap[i] = top[-i]; \
return v; \
} \
else{ \
for(i=0 ; i<HEAP; ++i)top[-i] = heap[i]; \
} \
tag; \
})
struct tuple f(){
const int v = timetravel(((struct tuple){1,ask_depth}));
printf("now %d\n",v);
if(v == N){
return (struct tuple){v,answer};
}
else {
const struct tuple p = f();
if(p.snd == 0){
return (struct tuple){p.fst + 1,ask_depth};
}
else{
return (struct tuple){p.fst + v,answer};
}
}
}
int main()
{
top = __builtin_frame_address(0);
const struct tuple p = f();
if(p.snd == ask_depth){
longjmp(env,p.fst);
}
else{
printf("sum = %d\n",p.fst);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment