[Question] yiben Tong 1.3 Exercise 1 Egyptian score

Source: Internet
Author: User

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

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.