Title: EOJ1154 CAN you DFS? ida* Search algorithm
Description
In ancient Egypt, people used unit fractions and (like 1/a, A is the natural number) to represent all rational numbers.
such as: 2/3=1/2+1/6, but do not allow 2/3=1/3+1/3, because addend have the same.
For a fraction A/b, there are many ways to express it, but which is best?
First of all, addend less than addend more good, second, add a number of the same, the smallest score bigger the better.
Such as:
19/45=1/3 + 1/12 + 1/180
19/45=1/3 + 1/15 + 1/45
19/45=1/3 + 1/18 + 1/30,
19/45=1/4 + 1/6 + 1/180
19/45=1/5 + 1/6 + 1/18.
The best is the last one, because 1/18 is bigger than 1/180,1/45,1/30,1/180.
Give a B (0〈a〈b〈1000), the best expression of programming calculation.
Input
Each group of test data is a row containing a, B (0〈a〈b〈1000).
Output
The number of test data per group, from childhood to large arrangement, followed by the denominator of the unit score. A set of solutions with the smallest output dictionary order.
Sample Input
19 45
Sample Output
5 6 18
Title Analysis: Test instructions gives a true score and splits it into one of several numbers. Priority requirements are as follows: 1, as few as possible. 2, the denominator of the last score is as large as possible. 3, the denominator dictionary order as small as possible.
Main idea: Search all cases by dictionary order, if you can combine the desired score and the last denominator is smaller, update the answer.
The main difficulty of this problem is the determination of the search margin ... A number can be split into countless numbers. Because of the need to divide the number of numbers as little as possible, so you can limit the depth of the search, gradually increase the depth of search, know to find the answer. This is the iterative deepening of the search idea.
At the same time require the denominator gradually become larger, that is, fractions gradually become smaller, if the subsequent scores are infinitely close to the current last score but can not be found in the specified depth of the correct answer, it is necessary to prune, this is a * algorithm idea.
This question should synthesize the above two kinds of thought.
To sum up, the basic idea of ida* is: first set the H value of the initial state node to the threshold Maxh, then the depth first search, the search process ignores all the H value is greater than the MAXH node; If no solution is found, increase the threshold maxh and repeat the search until a solution is found. Under the requirement that the calculation of H value satisfies a A * algorithm, it is proved that the solution found must be the optimal solution
AC Code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace Std;
Long Long ans[1500],v[1500];
int maxd;
Long Long gcd (long long A,long longb) {//for GCD for approximately differentiated simplicity
Return!B?A:GCD (B,A%B);
}
Long Long Get_first (long long A,long long B) {//to spell b per cent, maximum value of first fraction, denominator minimum
Long Long c=b/a;
if (b%a) ++c;
return C;
}
BOOL Dfs (Long long a,long long b,long longfrom,int deep) {
if (deep==maxd) {//reach the specified maximum search depth, end the current search
if (a!=1)
return false;
V[deep]=b;
if (V[deep]<ans[deep] | | ans[deep]==-1)//Determine if the solution is more optimal
memcpy (ans,v,sizeof (Long Long) * (deep+2));
return true;
}
BOOL Ok=false;
for (int i=from;; ++i) {
if ((maxd-deep+1) *b<=a*i) break;//cannot find solutions, pruning, a * algorithm in the current defined depth of the idea
V[deep]=i;
Long Long aa=a*i-b,bb=b*i;
int C=GCD (AA,BB);
Aa/=c;bb/=c;
From=max (Long Long) (i+1), Get_first (AA,BB));//start of Next search
if (Dfs (aa,bb,from,deep+1)) ok=true;
}
return OK;
}
int main ()
{
Long Long A, b;
while (~SCANF ("%i64d%i64d", &a,&b)) {
Long Long C=GCD (A, b);
A/=c;b/=c;
for (maxd=1;; ++maxd) {//Gradually increase recursion depth, iterative deepening
memset (ans,-1,sizeof (ans));
if (Dfs (A,b,get_first (b), 1)) {
for (int i=1;i<=maxd;++i) {
if (i>1) printf ("");
printf ("%i64d", Ans[i]);
}
printf ("\ n");
Break
}
}
}
return 0;
}
EOJ1154 CAN you DFS? ida* Search algorithm