CF134B Pairs of Numbers 题解
题意
分析
爆搜即可。如果从 \((1,1)\) 开始搜,显然会 TLE,所以可以枚举最终的状态,倒推 dfs 得到 \((1,1)\),打擂台取最少步数。对于一个状态 \((a,b)\),如果 \(a>b\),说明是从 \((a'+b',b')\) 扩展得到的,所以就倒推回 \((a',b')\),也就是 \((a-b,b)\);同理,如果 \(a,就倒推回 \((a,b-a)\),如果 \(a=1,b=1\) 就比较当前步数与当前最少的步数,更新答案。
代码
#include
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int MAXN=1e5+7;
inline void qread(){}template
inline void qread(T1 &a,T2&... b)
{
register T1 x=0;register bool f=false;char ch=getchar();
while(ch<'0') f|=(ch=='-'),ch=getchar();
while(ch>='0') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
x=(f?-x:x);a=x;qread(b...);
}
templateinline T1 qmax(const T1 &x,const T2 &y){return x>y?x:y;}
templateinline T1 qmin(const T1 &x,const T2 &y){return xb) dfs(a-b,b,c+1); // 从(a'+b',b')扩展来
if(b>a) dfs(a,b-a,c+1); // 从(a',a'+b')扩展来
}
int main()
{
qread(n);
for(int i=1;i<=n;i++) dfs(i,n,0),dfs(n,i,0); //枚举每一个最终状态
printf("%d\n",ans);
return 0;
}