版本比较
题解:P15632 [2019 KAIST RUN Spring] A Plus Equals B
正文
增加 44 行删除 12 行
43 行未改动4445464748484950515227 行未改动808182838484858687888990919293949536 行未改动13213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816913917017114117217314317417514514617617717817914918018118215218318418515518618718815818919019119219319419516519619719819920043 行未改动
void out(){ cout<<ans.size()<<"\n"; for(pr i:ans) cout<<i.first<<"+="<<i.second<<"\n";} }
void solve(int x,int y){ if(x==y){out();return;} if(!(x&1)&&!(y&1)) solve(x>>1,y>>1);27 行未改动我们采用启发式搜索类似思想,将上文做法作为估价函数,每一步采用四种操作中前瞻步数最小的一个操作。
当然,你的决策也影响你的步数。例如可以只在 $x,y$ 均为奇数的时候考察四种操作,找到前瞻步数最小的一个。
目前笔者比较了四种决策,各有优劣,其中第五个点最优秀的是 $322$ 步。当前,你的估价函数可以嵌套,例如将估价函数设置成刚才的做法。但是需要注意的时,若嵌套了 $x$ 层,则复杂度为 $O(ans^x)$。
目前笔者比较了五种决策,各有优劣,其中,嵌套两层的估价函数偏序了其他三个决策,几乎偏序第四个决策,第五个点仅用了 $306$ 步。
目前不会证明,因为类似这种设置估价函数的搜索的证明都非常困难。这里只是给出这种做法作为参考。
时间复杂度 $O(ans^x)$,$x$ 取决估价函数的嵌套层数。
## One Of Decisions
```cpp#include <bits/stdc++.h>36 行未改动 Solve1(x,y); return ans1;}
int ans2;
void solve1(int x,int y){ if(x==y) return; if(!(x&1)&&!(y&1)) solve1(x>>1,y>>1); else{ int p1=Solve(x<<1,y),p2=Solve(x+y,y); int p3=Solve(x,y<<1),p4=Solve(x,y+x); ans2++; if(p1<=p2&&p1<=p3&&p1<=p4){ solve1(x<<1,y); }else if(p2<=p1&&p2<=p3&&p2<=p4){ solve1(x+y,y); }else if(p3<=p2&&p3<=p1&&p3<=p4){ solve1(x,y<<1); }else{ solve1(x,y+x); } }}
int solve(int x,int y){ ans2=0; solve1(x,y); return ans2;}
#define pr pair<char,char>
vector<pr> ans;
void out(){ cout<<ans.size()<<"\n"; for(pr i:ans) cout<<i.first<<"+="<<i.second<<"\n";} }
void solve(int x,int y){void SOLVE(int x,int y){ if(x==y){out();return;} if(!(x&1)&&!(y&1)) solve(x>>1,y>>1); if(!(x&1)&&!(y&1)) SOLVE(x>>1,y>>1); else{ int p1=Solve(x<<1,y),p2=Solve(x+y,y); int p3=Solve(x,y<<1),p4=Solve(x,y+x); int p1=solve(x<<1,y),p2=solve(x+y,y); int p3=solve(x,y<<1),p4=solve(x,y+x); if(p1<=p2&&p1<=p3&&p1<=p4){ ans.push_back({'A','A'}); solve(x<<1,y); SOLVE(x<<1,y); }else if(p2<=p1&&p2<=p3&&p2<=p4){ ans.push_back({'A','B'}); solve(x+y,y); SOLVE(x+y,y); }else if(p3<=p2&&p3<=p1&&p3<=p4){ ans.push_back({'B','B'}); solve(x,y<<1); SOLVE(x,y<<1); }else{ ans.push_back({'B','A'}); solve(x,y+x); SOLVE(x,y+x); } }}
signed main(){ int x=read(),y=read(); solve(x,y); SOLVE(x,y); return 0;}
```