题解:B3753 [信息与未来 2019] 堆栈计算机
huangzhiyuanhzy · · 题解
这道题目并不是很难,思路如下:通过一系列操作生成一个长度为
先初始状态,将数列设为空。再选择操作,我们可以通过 dup 操作复制数列尾部的数字,通过 add 操作将数列尾部的两个数字相加并放回数列。
对于给定的数字 dup 和 add 操作来得到它。如果
所以,我们可以采用递归的思路来生成操作序列。具体来说,我们可以从目标数字 dup 和 add 操作得到这个数字。
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,x=1,s=-1;
bool f=1;
int main(){
cin>>n;
cout<<1<<endl;
while(x<n){
s+=bool(x&n);
if(x&n){
cout<<"dup"<<endl;
}
n-=n&x;
x<<=1;
if(x<=n){
cout<<"dup"<<endl;
cout<<"add"<<endl;
}
}
while((s--)>=0){
cout<<"add"<<endl;
}
return 0;
}