当前位置:网站首页>Acwing 4300. Two operations
Acwing 4300. Two operations
2022-07-05 05:20:00 【hunziHang】
Given a positive integer n, We hope you can through a series of operations , Change it to another positive integer m.
There are two kinds of operations :
1. Multiply the current number by 2
2. Subtract... From the current number 1
requirement , During the transformation , The number is always positive .
Please calculate , The minimum number of operations required .
Input format :
a line , Two different positive integers n and m.
Output format :
An integer , Indicates the minimum number of operations required .
Data range :
front 6 Test points meet 1≤n,m≤10.
All test points meet 1≤n,m≤10000.
sample input 1:
4 6
sample output 1:
2
sample input 2:
10 1
sample output 2:
9
1. greedy
prove : When n>m when , And m For even when , be /2.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+100;
typedef pair<int,int> PII;
#define x first
#define y second
#define INF 0x3f3f3f3f
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
const int mod=1e9+7;
int n,m;
int cnt;
int main()
{
cin>>n>>m;
if(n>=m) cout<<n-m<<endl;
else // Back to
{
while(n<m) //n>=m It can be converted to the first two cases
{
if(m&1) m++; // If m It's odd , Can not /2,n*2 The number changed must be even .
else m/=2; // An even number is ok /2.
cnt++;
}
cout<<cnt+n-m<<endl; // Switch to the first two cases
}
return 0;
}2. Search for Wide search
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=20100;
typedef pair<int,int> PII;
#define x first
#define y second
#define INF 0x3f3f3f3f
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
const int mod=1e9+7;
int n,m;
int d[N],q[N];
int main()
{
cin>>n>>m;
int hh=0,tt=0;
memset(d,0x3f,sizeof d); // Initial array , Easy to update
d[n]=0; // Starting from 0.
q[0]=n; // The starting point joins the queue .
while(hh<=tt)
{
int t=q[hh++];
int change[]={t-1,t*2}; // Use an array to represent two operations , Convenient operation
for(auto i:change)
{
if(i>=1&i<N&&d[i]>d[t]+1) // 2*n<2*m Maximum enumeration to 2e4
{
d[i]=d[t]+1; // If the conditions are met, it will be updated
q[++tt]=i; // Join the queue
}
}
}
cout<<d[m]<<endl;
return 0;
}边栏推荐
- Heap sort summary
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- PMP考试敏捷占比有多少?解疑
- 发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- [to be continued] [UE4 notes] L2 interface introduction
- National teacher qualification examination in the first half of 2022
- cocos_ Lua loads the file generated by bmfont fnt
- win10虚拟机集群优化方案
- Haut OJ 1321: mode problem of choice sister
猜你喜欢
![[turn to] MySQL operation practice (III): table connection](/img/70/20bf9b379ce58761bae9955982a158.png)
[turn to] MySQL operation practice (III): table connection

Download and use of font icons

C language Essay 1

YOLOv5添加注意力机制

object serialization

Magnifying glass effect

支持多模多态 GBase 8c数据库持续创新重磅升级

Quick sort summary

Merge sort

【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
随机推荐
cocos_ Lua listview loads too much data
Insert sort
嵌入式数据库开发编程(六)——C API
On-off and on-off of quality system construction
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Unity ugui source code graphic
Embedded database development programming (V) -- DQL
小程序直播+电商,想做新零售电商就用它吧!
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Service fusing hystrix
[allocation problem] 455 Distribute cookies
MySQL数据库(一)
room数据库的使用
win10虚拟机集群优化方案
To be continued] [UE4 notes] L4 object editing
Introduction to tools in TF-A
[binary search] 69 Square root of X
Simple HelloWorld color change
xftp7与xshell7下载(官网)
Data is stored in the form of table