第一眼看到这个题目(汉诺塔),应该一眼能看出来是递推吧。
那让我们来模拟一下整体的过程。
-
在 时,要挪两次(一眼解);
-
在 时,可以将问题进行拆分:
- 设 为 个圆盘的最少挪动次数,
- 可知先挪 个圆盘至B柱需要 次,
- 将剩下的两个扔到C柱需要 次,
- 将B柱的盘挪过来又需要 次。
所以,
公式为:
然鹅,
只有 70pts。
Why?
不开 long long 见祖宗,开了 long long 照样见祖宗。
看数据范围,发现 有可能到达 ,所以我们还需要高精度。
这就不用再写了吧,直接上代码(其实我想用 Python 糊弄一下的):
#include <bits/stdc++.h>
using namespace std;
#define int long long
string add(string a,string b){//高精加,对照公式里的加法。
string res;
int flag=0;
int i=a.size()-1,j=b.size()-1;
while(i>=0||j>=0||flag){
int sum=flag;
if(i>=0)sum+=a[i--]-'0';
if(j>=0)sum+=b[j--]-'0';
res.push_back(sum%10+'0');
flag=sum/10;
}
reverse(res.begin(),res.end());
return res;
}
string cheng(string a){//高精乘同理。
string res;
int flag=0;
for(int i=a.size()-1;i>=0;i--){
int product=(a[i]-'0')*2+flag;
res.push_back(product%10+'0');
flag=product/10;
}
if(flag)res.push_back(flag+'0');
reverse(res.begin(),res.end());
return res;
}
int n;
signed main(){
cin>>n;
string A="2";
for(int i=2;i<=n;i++){
A=cheng(A);
A=add(A,"2");
}
cout<<A;
return 0;
}Good!