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
Inputcopy | Outputcopy |
---|---|
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(); } }