Test instructions
The classic Egyptian score problem, which is to give a true fraction, is to use the smallest number of unit fractions to represent this score. If there are multiple scenarios, make each score as large as possible, that is, the denominator as small as possible. There will be K prohibited unit fractions.
Analysis:
ida* algorithm. When in the order of the denominator increment, if the current considered score is 1/e, the remaining maxd-d+1 layer is 1/e, but still cannot reach the target of A/b, the pruning.
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace Std;
typedef long Long LL;
const int maxn=10010;
int t;
ll N,m,k;
ll ANS[MAXN],V[MAXN];
set<ll>del;
ll GetFirst (ll A,ll b)
{
ll c= (b/a) +1;
return C;
}
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;
}
ll GCD (ll A,ll b)
{
Return B?GCD (b,a%b): A;
}
BOOL Dfs (ll a,ll b,ll start,int d,int maxd)
{
if (D==maxd)
{
cout<<maxd<<endl;
if (b%a)
return false;
v[d]=b/a;
if (Del.count (b/a))
return false;
if (better (d))
memcpy (ans,v,sizeof (LL) * (d+1));
return true;
}
BOOL Ok=false;
for (ll I=max (Start,getfirst (b));; ++i)
{
Cout<<i<<endl
if (b* (maxd+1-d) <=i*a)
Break
if (Del.count (i))
Continue
V[d]=i;
ll B1=b*i;
ll A1=a*i-b;
ll G=GCD (A1,B1);
if (Dfs (A1/G,B1/G,I+1,D+1,MAXD))
Ok=true;
}
return OK;
}
int main ()
{
scanf ("%d", &t);
for (int cas=1;cas<=t;cas++)
{
Del.clear ();
scanf ("%lld%lld%lld", &n,&m,&k);
int i,j;
ll Del0;
while (k--)
{
scanf ("%lld", &del0);
Del.insert (del0);
}
int maxd;
for (maxd=0;; maxd++)
{
cout<<maxd<<endl;
memset (ans,-1,sizeof (ans));
if (Dfs (N,m,getfirst (n,m), 0,maxd))
Break
}
printf ("Case%d:%lld/%lld=", cas,n,m);
for (I=0;i<=maxd;++i) {
if (i) printf ("+");
printf ("1/%lld", Ans[i]);
}
printf ("\ n");
}
}
UVA 12558 Egyptian fractions (hard version)