这题可以使用bfs的做法,特记录在此。
x点到mod的操作数可以视为其最短路。于是可用宽搜从1到mod建图。
有边权为(x+1)%mod 和(2*x)%mod 两种边权。
所以每次求数x到mod的最下操作数变为到mod这个顶点的最小边数。
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
#define si set<int>
#define mi map<int,int>
#define mc map<char,int>
#define YES cout<<"Yes"<<endl;
#define NO cout<<"No"<<endl;
#define pb(x) push_back(x);
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
const int mod=32768;
int ans[mod];
typedef struct node{
int x,step;
}NODE;
queue<NODE>q;
int st[mod];
vector<int> v[mod];
void bfs()
{
NODE tmp={0,0},t1;
q.push(tmp);
st[0]=1;
while(q.size()){
t1=q.front();
q.pop();
for (auto i:v[t1.x] ){
if(!st[i]){
st[i]=1;
ans[i]=ans[t1.x]+1;
tmp={i,ans[i]};
q.push(tmp);
}
}
}
}
void solve()
{
for (int i=1;i<mod;i++){
v[(i+1)%mod].push_back(i);
v[(2*i)%mod].push_back(i);
}
bfs();
int n;
cin>>n;
for (int i=1;i<=n;i++){
int x;
cin>>x;
cout<<ans[x]<<" ";
}
cout<<endl;
}
signed main()
{
IOS
int t;
t=1;//cin>>t;
while(t--){
solve();
}
}