CF1560D Make a Power of Two 题解

文章提供了对编程竞赛题目CF1560D的解答,该题要求通过有限次操作将一个整数变成2的非负整数次幂。操作包括删除数位和在右侧添加数位。题目解析了如何计算达到目标状态所需的最小操作次数,并给出了样例输入和输出以及代码实现。
摘要由CSDN通过智能技术生成

题目

链接

https://www.luogu.com.cn/problem/CF1560D

字面描述

题面翻译

给定一个整数 n n n。每次操作你可以做两件事情中的一件:

  • 删去这个数中的一个数位(如果这个数只剩下一位,则可以把它删空)。
  • 在这个数的右边添加一个数位。

你可以以任意顺序执行无限次操作。但请注意,在删去一个数位之后,这个数可能包含前导零(例如在删去 301 301 301 中的 3 3 3 这一位之后,这个数就会变成 01 01 01 而不是 1 1 1)。

你需要执行若干次操作,使得这个数最终变成一个 2 2 2 的次幂,或者说存在一个非负整数 k k k 使得这个数最终是 2 k 2^k 2k。最终答案不能包含前导零。请求出需要执行的操作的最小次数。

t t t 组数据, 1 ⩽ t ⩽ 1 0 4 1\leqslant t\leqslant 10^4 1t104 1 ⩽ n ⩽ 1 0 9 1\leqslant n\leqslant 10^9 1n109

题目描述

You are given an integer $ n $ . In $ 1 $ move, you can do one of the following actions:

  • erase any digit of the number (it’s acceptable that the number before the operation has exactly one digit and after the operation, it is “empty”);
  • add one digit to the right.

The actions may be performed in any order any number of times.

Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number $ 301 $ the digit $ 3 $ , the result is the number $ 01 $ (not $ 1 $ ).

You need to perform the minimum number of actions to make the number any power of $ 2 $ (i.e. there’s an integer $ k $ ( $ k \ge 0 $ ) such that the resulting number is equal to $ 2^k $ ). The resulting number must not have leading zeroes.

E.g. consider $ n=1052 $ . The answer is equal to $ 2 $ . First, let’s add to the right one digit $ 4 $ (the result will be $ 10524 $ ). Then let’s erase the digit $ 5 $ , so the result will be $ 1024 $ which is a power of $ 2 $ .

E.g. consider $ n=8888 $ . The answer is equal to $ 3 $ . Let’s erase any of the digits $ 8 $ three times. The result will be $ 8 $ which is a power of $ 2 $ .

输入格式

The first line contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. Then $ t $ test cases follow.

Each test case consists of one line containing one integer $ n $ ( $ 1 \le n \le 10^9 $ ).

输出格式

For each test case, output in a separate line one integer $ m $ — the minimum number of moves to transform the number into any power of $ 2 $ .

样例 #1

样例输入 #1
12
1052
8888
6
75
128
1
301
12048
1504
6656
1000000000
687194767
样例输出 #1
2
3
1
3
0
0
2
1
3
4
9
2

提示

The answer for the first test case was considered above.

The answer for the second test case was considered above.

In the third test case, it’s enough to add to the right the digit $ 4 $ — the number $ 6 $ will turn into $ 64 $ .

In the fourth test case, let’s add to the right the digit $ 8 $ and then erase $ 7 $ and $ 5 $ — the taken number will turn into $ 8 $ .

The numbers of the fifth and the sixth test cases are already powers of two so there’s no need to make any move.

In the seventh test case, you can delete first of all the digit $ 3 $ (the result is $ 01 $ ) and then the digit $ 0 $ (the result is $ 1 $ ).

思路

本题最终的目的是将一个[1,1e9]的整数通过2种操作变为2的非负整数次幂。

将20 - 255 每一个数位上的数打表预处理,用原数列一一与表中元素比较,计算操作次数,记录最小值。

时间复杂度: O ( 10 t ) ≈ 1 e 5 O(10t)≈1e5 O(10t)1e5

代码实现

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=100;
const int inf=2e9;
int t,ans=inf,tot;
ll op=1,x;
ll a[maxn];
int k[maxn];//记录表中每一元素的长度
ll cnt[maxn][maxn];
int main(){
	//预处理
	++k[0];
	cnt[0][1]=1;
	for(int i=1;i<=55;i++){
		op=(ll)op*2;
		ll op1=op;
		while(op1){
			cnt[i][++k[i]]=(ll)op1%10;
			op1=(ll)op1/10;
		}
	}
	/*
	for(int i=1;i<=40;i++){
		for(int j=k[i];j>=1;j--)printf("%lld",cnt[i][j]);
		printf("\n");
	}
	*/
	scanf("%d",&t);
	while(t--){
		ans=inf;
		scanf("%lld",&x);
		tot=0;
		while(x){
			a[++tot]=(ll)x%10;
			x=(ll)x/10;
		}
		//for(int i=tot;i>=1;i--)printf("%d",a[i]);
		//计算比较
		for(int i=0;i<=55;i++){
			int st1=tot,st2=k[i];
			while(st1>0&&st2>0){
				if(a[st1]==cnt[i][st2])--st2;
				--st1;
			}
			//if(i==41)printf("41 %d %d\n",k[i],st2);
			ans=min(ans,tot-k[i]+2*st2);// tot-(k[i]-st2) 删除的操作次数操作次数 & st2 添加的操作次数
		}
		printf("%d\n",ans);
	}
	return 0;
}

备注

写入好题本

CF2004A Closet Point是一个常见的数据结构题,通常涉及最小化操作次数的问题。题目描述的是在一个二维网格上找到最短距离的点对,每次可以沿着水平、垂直或对角线移动一格。这个题目常常可以用哈希表(Disjoint Set Union,简称DSU)或者最近点对搜索(Floyd-Warshall Algorithm)来解决。 C++题解概述: ```cpp #include <iostream> #include <vector> #include <set> using namespace std; const int inf = (int)1e9 + 7; vector<int> x[2005], y[2005]; set<pair<int, int>> dist; void init(int n) { for (int i = 1; i <= n; i++) { x[i].clear(); y[i].clear(); dist.insert({i, i}); } } bool sameSet(int u, int v) { return dist.find({u, v}) != dist.end(); } void merge(int u, int v) { if (!sameSet(u, v)) { dist.erase({min(u, v), max(u, v)}); dist.insert({u, v}); } } void dfs(int node, int start, vector<vector<int>>& dp) { dp[node][start] = min(dp[node][start], abs(x[node][start] - x[start][start]) + abs(y[node][start] - y[start][start])); for (auto &p : {x[node], y[node]}) { for (int i = 0; i < p.size(); ++i) { if (i == start || !sameSet(p[i], start)) continue; dfs(node, i, dp); merge(p[i], start); } } } int main() { int n; cin >> n; init(n); for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) { cin >> x[i][j]; for (int k = 1; k < n; k++) { y[k + i][j] = x[i][j]; } } } int m; cin >> m; vector<vector<int>> dp(n + 1, vector<int>(n + 1, inf)); while (m--) { int a, b; cin >> a >> b; if (!sameSet(a, b)) { merge(a, b); } dfs(a, b, dp); } cout << *dist.begin() << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值