#include #include #include typedef long long int int64; #define MAX(a,b) ((a)>(b)?(a):(b)) #define MIN(a,b) ((a)<(b)?(a):(b)) #define ABS(a) ((a)>(0)?(a):-(a)) typedef struct fraction{ int64 nu; int64 de; } frac; int64 gcd(int64 a,int64 b){ int64 r=a%b; while(r>0){ a=b;b=r;r=a%b; } return b; } void add(frac *c,frac *a,frac *b){ int64 x=a->nu*b->de+a->de*b->nu; if(x==0){ c->nu=0; c->de=1; return; } int64 y=a->de*b->de; int64 g=gcd(ABS(x),y); c->nu=x/g; c->de=y/g; return; } void sub(frac *c,frac *a,frac *b){ int64 x=a->nu*b->de-a->de*b->nu; if(x==0){ c->nu=0; c->de=1; return; } int64 y=a->de*b->de; int64 g=gcd(ABS(x),y); c->nu=x/g; c->de=y/g; return; } void mul(frac *c,frac *a,frac *b){ int64 x=a->nu*b->nu; if(x==0){ c->nu=0; c->de=1; return; } int64 y=a->de*b->de; int64 g=gcd(ABS(x),y); c->nu=x/g; c->de=y/g; return; } void division(frac *c,frac *a,frac *b){ int64 x=a->nu*b->de; if(x==0){ c->nu=0; c->de=1; return; } int64 y=a->de*b->nu; int64 g=gcd(ABS(x),y); c->nu=x/g; c->de=y/g; return; } frac stack[7]; int test; int b[6]; int num; int calc(int top,int k){ if(top==1 && stack[0].nu==test && stack[0].de==1) return 1; if(top==1 && k==0) return stack[0].nu==test && stack[0].de==1; //operator if(top>=2){ frac a=stack[top-1]; frac b=stack[top-2]; add(stack+top-2,&a,&b);if(calc(top-1,k)) return 1; sub(stack+top-2,&a,&b);if(calc(top-1,k)) return 1; mul(stack+top-2,&a,&b);if(calc(top-1,k)) return 1; if(b.nu!=0) division(stack+top-2,&a,&b);if(calc(top-1,k)) return 1; stack[top-1]=a; stack[top-2]=b; } //push int i; for(i=0;i