1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #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(a,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; }
|