LOJ P10022 Egyptian fraction solution to a problem

Daily questions day62 punch

Analysis

This question is feeling a lot like watching a search, but each enumeration x∈ (1,10000000) as the denominator is obviously stupid.

So we find a way to optimize the code.

Optimization of a: iterative deepening

 

Optimization II:

We determined the search, the search is now necessary to determine the upper and lower bounds.

Because now search for a certain value than the remaining fraction is small, so there are:

1/i​<x/y

Satisfies the boundary Y < X I

There are set of k scores, because the enumeration of the denominator is monotonically increasing, so the value of the score is monotonically decreasing, the value of k can be obtained fraction is strictly less than k / i, and this value is necessarily better than the rest of the current great value,

Then there are:

k / i> x / y
lower bounds satisfies  K Y > X I

 

Optimization think it over and then you can feel at ease code Code ~

Note: Some details have comments in the code

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define int long long
 6 #define INF 2147483647
 7 #define maxn 100000+10
 8 #define rep(i,s,e) for(register int i=s;i<=e;++i)
 9 #define dwn(i,s,e) for(register int i=s;i>=e;--i)
10 using namespace std;
11 inline int read()
12 {
13     int x=0,f=1;
14     char c=getchar();
15     while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
16     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
17     return f*x;
18 }
19 void write(int x)
20 {
21     if(x<0) {putchar('-'); x=-x;}
22     if(x>9) write(x/10);
23     putchar(x%10+'0');
24 }
25 int a,b;
26 int t,len;
27 int s[maxn],ans[maxn];
28 inline int calc(int x,int y)
29 {
30     rep(i,2, INF)
 31 is          IF (I * X> Y)
 32              return I;
 33 is  }
 34 is  bool DFS ( int REST, int X, int Y) // because the length is determined to be convenient enumeration Can, so the choice of type of search bool 
35  {
 36      IF (REST == . 1 )
 37 [      {
 38 is          IF (X == . 1 && Y> S [T] && (len == 0 || Y < ANS [len]))
 39          {
 40              len = ++ T;
 41 is              S [T] = Y;
 42 is             rep(i,1,t) ans[i]=s[i];
43             --t;
44             return true;
45         }
46         return false;
47     }
48     bool flag=false;
49     for(register int i=max(s[t]+1,calc(x,y));rest*y>i*x;++i)
50     {
51         int now_x=x*i-y;
52         int now_y=y*i;//nx/xy=x/y-1/i; 
53         int mod=__gcd(now_x,now_y);
54         now_x/=mod;
55         now_y/=mod;
56         s[++t]=i;
57         if(dfs(rest-1,now_x,now_y)==true) flag=true; 
58         --t;
59     }
60     return flag;
61  } 
62 signed main()
63 {
64     a=read();b=read();
65     int mod=__gcd(a,b);
66     a/=mod;
67     b/=mod;
68     if(a==1) {write(b); return 0;}
69     s[0]=1;
70     rep(i,2,INF)
71     {
72         t=0;
73         if(dfs(i,a,b)==true)
74         {
75             rep(j,1,len) 
76             {
77                 write(ans[j]); 
78                 putchar(' ');
79             }
 80              BREAK ; // first answer must be found to the optimal 
81          }    
 82      }
 83      return  0 ;
 84 }

If mistakes please correct me big brother(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/12076449.html