UVA 12558 Egyptian fractions (hard version)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.