Egyptian score question _ iterative deepening search _ C ++, Egyptian _ c

Source: Internet
Author: User

Egyptian score question _ iterative deepening search _ C ++, Egyptian _ c

I. Question Background

Http://codevs.cn/problem/1288/

A real score is given, and the score in the form of least 1/a is used to indicate the true score. If the number is the same, the minimum score is the maximum, and each score is different.

For example, 19/45 = 1/3 + 1/12 + 1/180

Ii. Iterative deepening search

Iterative deep search can be seen as a DFS with deep limitations.

First, set a search depth and then perform DFS. When the current depth reaches the limit depth, verify the rationality of the current solution and update the answer.

Adjust the search depth until you find the optimal solution.

Iii. Implementation of Egyptian scores

We use dep to limit the number of layers to search. The first step is from 2. Each depth is + 1.

When searching, each layer has a lower score than the previous layer, that is, the denominator is larger than the previous layer.

After each quote 1/a, subtract the current score and recursively pass the remaining score.

The following conditions apply to each layer of search enumeration:

1. Ensure that the current depth denominator is greater than the previous depth denominator

2. If the value of 1/a in the enumeration is smaller than the current score, there cannot be an equal state, because this optimal solution will appear when the depth of the enumeration is small.

3. Set the current remaining score to x/y and the remaining depth to d, then x/y <d/a → a <d/x * y.

If the denominator of the enumerated values is a, then the number is exactly x/y, but the score cannot be equal. Therefore, a must be smaller than this value to increase the score. If the score is small, the target score will never be reached.

When the depth reaches the limit depth, you only need to determine whether the remaining score meets the 1/a format, and then update the result.

Remember to enable long and use % lld for output, because the data will be large during the general split.

The time complexity is roughly the same as the search. Because the time complexity of the current restricted depth is much greater than the time complexity of the last restricted depth, the time of the previous depth can be ignored.

Advantage: Low space expenditure

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #define N 1000 8 using namespace std; 9 10 long long ans[N],s[N],mo,ch;11 int dep;12 long long gcd(long long a,long long b){return b==0?a:gcd(b,a%b);}13 void outp()14 {15     int i;16     if (ans[dep]>s[dep])17         {18             for (i=1;i<=dep;i++)19             {20                 ans[i]=s[i];21             }22         } 23 }24 void dfs(long long x,long long y,int d)25 {26     long long a,b,i,w;27     if (d==dep)28     {29         s[d]=y;30         if ((x==1)&&(s[d]>s[d-1])) outp();31         return;32     }33     for (i=max(s[d-1]+1,y/x+1);i<(dep-d+1)*y/x;i++)34     {35         b=y*i/gcd(y,i);36         a=b/y*x-b/i;37         w=gcd(a,b);38         a/=w;39         b/=w;40         s[d]=i;41         dfs(a,b,d+1);42     }43 }44 int main()45 {46     int i=0,j;47     scanf("%d%d",&ch,&mo);48     i=gcd(ch,mo);49     ch/=i;50     mo/=i;51     for (dep=2;;dep++)52     {53         ans[1]=0;54         s[0]=0;55         ans[dep]=2000000000;56         dfs(ch,mo,1);57         if (ans[1]!=0) break;58     }60     for (j=1;j<=dep;j++)61     {62         printf("%lld ",ans[j]);63     }64     printf("\n");65     return 0;66 }

 

 

 

All rights reserved. For details, contact the author.

QQ: 740929894

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.