A - 超简单的哦

 

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

  • either set v = (v + 1) \bmod 32768v=(v+1)mod32768
  • or set v = (2 \cdot v) \bmod 32768v=(2⋅v)mod32768.

You are given nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​. What is the minimum number of operations you need to make each a_iai​ equal to 00?

Input

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

The second line contains nn integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (0 \le a_i < 327680≤ai​<32768).

Output

Print nn integers. The ii-th integer should be equal to the minimum number of operations required to make a_iai​ equal to 00.

Sample 1

Inputcopy Outputcopy
4
19 32764 10240 49
14 4 4 15 

Note

Let's consider each a_iai​:

  • a_1 = 19a1​=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.
  • a_2 = 32764a2​=32764. You can increase it by one 44 times: 32764 \rightarrow 32765 \rightarrow 32766 \rightarrow 32767 \rightarrow 032764→32765→32766→32767→0.
  • a_3 = 10240a3​=10240. You can multiply it by two 44 times: 10240 \rightarrow 20480 \rightarrow 8192 \rightarrow 16384 \rightarrow 010240→20480→8192→16384→0.
  • a_4 = 49a4​=49. You can multiply it by two 1515 times.

分析,广度优先搜索,但是注意剪枝,这个数出现过就标记,其次,再开一个桶,把出现的答案全存一遍,如果防止重复数据,超时

#include 
#include 
#include 
#include 
#include 
using namespace std;
constexpr int mod = 32768, N = 40007;
int bfs(int u) {
    int step=0;
    if(u%mod==0)return 0;
    bitsetst=0;
    queue>q;
    q.push(make_pair(u,0));
    while(!q.empty()){
        auto t=q.front();
        q.pop();
        int val1=(t.first<<1)%mod,val2=(t.first+1)%mod;
         step=t.second+1;
        if(val1==0||val2==0){
            return step;
        }
        if(st[val1]==0){
            q.push(make_pair(val1,step));
            st[val1]=1;
        }
        if(st[val2]==0){
            q.push(make_pair(val2,step));
            st[val2]=1;
        }
    }
    return -1;
}
void solve() {
   int n;
   vectorsti(N,-1);
    scanf("%d",&n);
    for(int i=0;i

 

你可能感兴趣的:(算法)