CF1661B Getting Zero
Getting Zero
题面翻译
给定 ,可以将 如下操作:
变成 或者 ,
求最少经过几次操作能将 变回 。
题目描述
Suppose you have an integer . In one operation, you can:
- either set
- or set .
You are given integers . What is the minimum number of operations you need to make each equal to ?
输入格式
The first line contains the single integer ( ) — the number of integers.
The second line contains integers ( ).
输出格式
Print integers. The -th integer should be equal to the minimum number of operations required to make equal to .
样例 #1
样例输入 #1
4
19 32764 10240 49
样例输出 #1
14 4 4 15
提示
Let's consider each :
- . You can, firstly, increase it by one to get and then multiply it by two times. You'll get in steps.
- . You can increase it by one times: .
- . You can multiply it by two times: .
- . You can multiply it by two times.
思路
32768实际上是2的15次方。看到最少次数的时候,可以想到bfs宽搜到最短路的性质,而两种操作方式对应的就是扩展方法,依次,我们就能求出来扩展的最少次数。
代码
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int dist[65537];
int bfs(int u){
memset(dist,-1,sizeof dist);
queue<int> q;
q.push(u);
dist[u]=0;
while(q.size()){
int t=q.front();
q.pop();
if(!t) return dist[0];
int f1=(t+1)%32768,f2=(t<<1)%32768;
if(!~dist[f1]&&dist[t]+1<=15){
dist[f1]=dist[t]+1;
q.push(f1);
}
if(!~dist[f2]&&dist[t]+1<=15){
dist[f2]=dist[t]+1;
q.push(f2);
}
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
bfs(x);
cout<<dist[0]<<" ";
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!