#include <cstdio> #include <cstring> #include <algorithm> #include <set>using namespace std; typedef long LONG Ll;const int maxn=10010;int maxd,t,tt;set<ll> SK; LL ANS[MAXN],V[MAXN]; ll GCD (ll A,ll b) {return b?gcd (b, a%b): A;} ll Get_first (ll A, ll b) {return b/a + 1;} BOOL Better (int d) {for (int i=d;i>=0;--i) if (V[i]!=ans[i]) return ans[i]==-1| | v[i]<ans[i]; return false;} BOOL Dfs (int d,ll from,ll A,ll b) {if (D==maxd) {if (b%a) return false; v[d]=b/a; if (Sk.count (b/a)) return false; if (better (d)) memcpy (ans,v,sizeof (LL) * (d+1)); return true; } bool OK =false; For (LL I=max (From,get_first (b));; ++i) {if (b* (maxd+1-d) <=i*a) break; if (Sk.count (i)) continue; V[d]=i; LL B2=b*i; LL A2=a*i-b; LL G=GCD (A2,B2); if (Dfs (d+1,i+1,a2/g,b2/g)) ok=true; } return OK; int main () {scanf ("%d", &t); while (t--) {LL a,b,k,sk0; Sk.clear (); scanf ("%lld%lld%lld", &a,&b,&k); while (k--) scanf ("%lld", &sk0), Sk.insert (Sk0); for (maxd=0;; ++maxd) {memset (ans,-1,sizeof (ans)); if (Dfs (0,get_first (A, b), A, b)) break; } printf ("Case%d:%lld/%lld=", ++tt,a,b); for (int i=0;i<=maxd;++i) {if (i) printf ("+"); printf ("1/%lld", Ans[i]); } printf ("\ n"); } return 0;}
12558-egyptian Fractions (hard version)