跳到正文

题解

P9502 『MGOI』Simple Round I | A. 魔法数字 题解

DengDuck约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月20日
篇幅
约 1 分钟
获赞
2
题目
P9502

版本

  1. v1当前

一种 O(1)\mathcal O(1) 没有循环的做法,可以说明我会数学,但不多。

2m<n2^m<n

两边同时取对数,得:

m<log⁡2nm<\log_2n

显然,由于我们需要满足小于这个条件,而 m,nm,n 为整数,因此我们可以推导出:

m≤log⁡2(n−1)m\leq \log_2(n-1)

由于我们最大化 mm,所以:

m=⌊log⁡2(n−1)⌋m=\lfloor\log_2(n-1)\rfloor

代码非常短。

C++
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL x;
int main()
{
	cin>>x;
	cout<<(long long)(log2(x-1)/2)*2;
}
原文