Describe
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.
Example: 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: a B
Output: Several numbers, from childhood to large permutations, in turn, the denominator of the unit fraction.
Example 1 Sample Input 1
19 45
Sample Output 1
5 6 18
Limit
Each test point 1s
(from https://vijos.org/p/1308)
This problem is obviously using search
(1) Deep Search, do not know the depth
(2) wide search, the amount of data stored may be too large
(3) Iterative deepening, pruning time should be able to
Then use the iteration to deepen
But there are some questions:
(1) Where to start enumerating the denominator
The lowest value of the previous denominator +1 for the countdown of the remaining fractions
The second one is well understood, as for the first one:
Assuming that A/b is left in front of you, using [b/a] as the denominator, the new score may be a little larger than the original score.
So you can do the starting condition.
(2) End for (max depth-current depth + 1) * (b/a) rounding
Code:
1 /**2 *vijos.org &codevs.cn3 *problem#1308 problem#12884 *accepted Accepted5 *time:528ms 741ms6 *memort:564k 256k7 */8#include <iostream>9#include <cstdio>Ten#include <cstring> One #defineint long Long A #definell Long Long - #define_max (A, B) (a>b)? (a):(B) - using namespacestd; the /** - * Defining Fractional classes - */ -typedefBOOLBoolean; +typedefclassmyclass{ - Public: + ints; A intm; atMyClass (): s (0), M (0){} -MyClass (intSintm): s (s), M (m) {} - #undefInt -MyClassoperator-(MyClass another) { - MyClass result; - intG=getcommon ( This-m,another.s); inresult.m= This->m*another.m/G; -result.s= This->s*another.m/g- This->m/g*Another.s; to intg1=Getcommon (RESULT.M,RESULT.S); +Result.m/=G1; -Result.s/=G1; the returnresult; * } $ Boolean empty () {Panax Notoginseng return(m==0); - } theBooleanoperator<(MyClass another) { + if(Another.empty ())return true; A if( This->empty ())return false; the if( This->S==ANOTHER.S)return This->m>another.m; + return( This->s*1.0/ This->M) < (another.s*1.0/another.m); - } $Booleanoperator< (Doubleanother) { $ if( This->empty ())return false; - return( This->s*1.0/ This->M) <another; - } theInlineintGetrint () { - //if (s==0) return-1;Wuyi return(int)( This->m/ This-s); the } - Boolean isworkable () { Wu if( This->empty ())return false; - return(m%s==0); About } $ void operator<< (IStream &inch){ - inch>>s>>m; - } - Private: A intGetcommon (ll a,ll b) { + if(b==0)returnA; the returnGetcommon (b,a%b); - } $ }myclass; the MyClass Maxx; the intresults[ the]; the intlist[ the]; the intFound =0; - in /** the * Iterative Deepening the */ About voidSearchintDepthlimit,intNow,myclass FS) { the if(fs<0)return ; the if(Now>depthlimit)return ; the if(Fs.isworkable () && (fs.m>list[now-1]) && (found==0|| maxx<FS)) { +maxx=FS; -results[depthlimit]=fs.m/FS.S; thememcpy (Results,list,sizeof(int)*depthlimit);BayiFound=Depthlimit; the return ; the } - for(intI=_max (Fs.getrint (), list[now-1]+1); I<= (depthlimit-now+1) *fs.getrint (); i++){ -list[now]=i; theSearch (depthlimit,now+1, Fs-myclass (1, i)); the } the } the intMain () { - MyClass Q; theq<<cin; the //cout<<q.getrint (); the intI=1;94list[0]=1; the while(found==0){ theSearch (I,1, q); thei++;98 } About for(intI=1; i<=found;i++){ -printf"%d", Results[i]);101 }102}
Iterative Deepening
Egypt score (Vijos 1308)