日志7:bfs一维运用

Suppose you have an integer v�. In one operation, you can:

  • either set v=(v+1)mod32768�=(�+1)mod32768
  • or set v=(2⋅v)mod32768�=(2⋅�)mod32768.

You are given n� integers a1,a2,…,an�1,�2,…,��. What is the minimum number of operations you need to make each ai�� equal to 00?

Input

The first line contains the single integer n� (1≤n≤327681≤�≤32768) — the number of integers.

The second line contains n� integers a1,a2,…,an�1,�2,…,�� (0≤ai<327680≤��<32768).

Output

Print n� integers. The i�-th integer should be equal to the minimum number of operations required to make ai�� equal to 00.

Sample 1

InputcopyOutputcopy
4
19 32764 10240 49
14 4 4 15 

Note

Let's consider each ai��:

  • a1=19�1=19. You can, firstly, increase it by one to get 2020 and then multiply it by two 1313 times. You'll get 00 in 1+13=141+13=14 steps.
  • a2=32764�2=32764. You can increase it by one 44 times: 32764→32765→32766→32767→032764→32765→32766→32767→0.
  • a3=10240�3=10240. You can multiply it by two 44 times: 10240→20480→8192→16384→010240→20480→8192→16384→0.
  • a4=49�4=49. You can multiply it by two 1515 times.

#include<bits/stdc++.h>
typedef long long ll;
#define endl '\n'
using namespace std;
int t;
int v,ans;
int vis[33000];
struct nmb
{
    int x,temp;
};  nmb a; nmb b; nmb c;
queue<nmb> q;
int main()
{
    ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
    cin>>t;
    while(t--)
    {
        cin>>v;
        a.x=v,a.temp=0;
        vis[v]=1;
        q.push(a);
        while(!q.empty())
        {
            a=q.front();
            q.pop();
            if(a.x%32768==0)
            {
                ans=a.temp;
                break;
            }
            if(vis[(a.x+1)%32768]==0)
            {
                b.x=(a.x+1)%32768;
                b.temp=a.temp+1;
                vis[(a.x+1)%32768]=1;
                q.push(b);
            }
            if(vis[(a.x*2)%32768]==0)
            {
                b.x=(a.x*2)%32768;
                b.temp=a.temp+1;
                vis[(a.x*2)%32768]=1;
                q.push(b);
            }
        }
        cout<<ans<<" ";
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值