Link to the question of the Egyptian score
This question is classic.
Algorithm: iterative deepening + IDA *
Optimization:
1. Iterative deepening
2. Determine the search order from small to large
3. Determine the search Upper and Lower Bounds
(1) The number with I as the denominator cannot be greater than a/B.
(2) If all the digits following are I as the denominator and still <= A/B, exit.
Details:
(1) int will be cracked during the general split process.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define ll unsigned long # define R registerusing namespace STD; const int n = 1e5 + 5; ll ans_a, ans_ B, ANS [N], LIN [N], K, flag, vis [N]; inline ll gcd (r ll, r LL B) {If (B = 0) return a; return gcd (B, A % B);} inline void DFS (r ll X, r ll, r LL B, ll lst) // enter the number of {If (x = k) {If (B % A = 0) {flag = 1; ans [k] = B/a; If (ANS [k] <LiN [k]) (R ll I = 1; I <= K; I ++) LiN [I] = ans [I];} return;} For (r ll I = lst + 1; i; I ++) {If (k-x + 1) * B <= A * I) break; if (I * A <B) continue; r ll Zi = I * A-B; r ll mu = I * B; r ll G = gcd (zi, Mu); ans [x] = I; DFS (x + 1, Zi/g, MU/g, I); ans [x] = 0 ;}} int main () {scanf ("% LLD ", & ans_a, & ans_ B); cn k = 0; k = gcd (ans_a, ans_ B); ans_a/= K; ans_ B/= K; k = 0; memset (Lin, 127, sizeof (Lin); While (! Flag) {k ++; DFS (1, ans_a, ans_ B, 0) ;}for (r ll I = 1; I <= K; I ++) printf ("% LLD", LIN [I]); Return 0;} // iterations deepen // upper and lower bounds pruning
[Question] yiben Tong 1.3 Exercise 1 Egyptian score