专栏文章

题解:P14054 [SDCPC 2019] Sekiro

P14054题解参与者 1已保存评论 3

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
3 条
当前快照
1 份
快照标识符
@minvex9d
此快照首次捕获于
2025/12/02 08:59
3 个月前
此快照最后确认于
2025/12/02 08:59
3 个月前
查看原文

题解:P14054 [SDCPC 2019] Sekiro

题目分析

通过简单的计算,可以发现最后的答案是 n2k\left\lceil \frac{n}{2^k} \right\rceil
考虑暴力,但是暴力的时间复杂度是 O(T×K)O(T\times K),毫无疑问会超时,怎么办呢?
这时候,聪明的你发现当 kk 很大时(比如 k>log2(n)k>\log_2(n) 时),值会稳定在 11。也就是说,当 kk 足够大时,结果就是 11
因为 nn 的取值范围 1n10910152501\le n\le 10^9\le 10^{15}\approx 2^{50},所以我们可以将每次输入分成几种情况讨论:
  • k50k\le 50 时,暴力求出答案。
  • k>50k>50 时,直接输出 11
  • 注意,当 n=0n=0 时,无论 kk 是多少,结果都是 00,这里需要特判。
接下来的代码就很简单了。

代码实现

代码:
CPP
//洛谷 P14054 [SDCPC 2019] Sekiro 
#include<bits/stdc++.h>
using namespace std;
//#define LOCAL//
#define emdl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int MAXN=1+5;

int n,k;

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	#ifdef LOCAL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	
	int t;
	cin>>t;
	while(t--){
		cin>>n>>k;
		
		//特判 n=0 
		if(n==0){
			cout<<"0"<<emdl;
			continue;
		}
		
		//贪心 
		if(k>50){
			cout<<"1"<<emdl;
			continue;
		}
		//暴力求出答案 
		int x=n;
		for(int i=1;i<=k;i++){
			x=(x+1)/2;
		} 
		cout<<x<<emdl;
	}

	return 0;
}
时间复杂度 O(T×K)O(T\times K),因为 kk 最大为 5050,所以时间复杂度为 O(T)O(T)

评论

3 条评论,欢迎与作者交流。

正在加载评论...