#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; }
时间: 2024-11-06 02:27:46