蓝桥杯小白赛第六期 6.计算方程 知识点:数学,二分

6.计算方程【算法赛】 - 蓝桥云课 (lanqiao.cn)

求x的最大范围

在不考虑 <math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ k x \log_{k}{x} </math>logkx 给 <math xmlns="http://www.w3.org/1998/Math/MathML"> x \sqrt{x} </math>x 增值的情况下:

因此,x最大不会超过m的平方+1,即 <math xmlns="http://www.w3.org/1998/Math/MathML"> x < = 1 e 8 x<=1e8 </math>x<=1e8。


现在我们再看看看 <math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ k x \log_{k}{x} </math>logkx会对x增值有多大影响: 因为k是样例输入的值,因为log函数底越小函数增长越快,我们假设k是2(1被排除了),这样求出来的 <math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ k x \log_{k}{x} </math>logkx值最大。

<math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ k x \log_{k}{x} </math>logkx

<math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ 2 1 e 8 \log_{2}{1e8} </math>log21e8

<math xmlns="http://www.w3.org/1998/Math/MathML"> 2 n = x 2^n=x </math>2n=x

<math xmlns="http://www.w3.org/1998/Math/MathML"> 2 n = 1 e 8 2^n=1e8 </math>2n=1e8

<math xmlns="http://www.w3.org/1998/Math/MathML"> n = 26 n=26 </math>n=26

因此 <math xmlns="http://www.w3.org/1998/Math/MathML"> log ⁡ k x = 26 \log_{k}{x}=26 </math>logkx=26

26可以忽略不计。

因此我们可以枚举x最大到1e8就可以了。

code

js 复制代码
#include<bits/stdc++.h>
using namespace std;

int k,m;

int check(int x)
{
  if(sqrt(x)+floor(log(x)/log(k))-m>0)return true;
  return false;	
}
void sovel()
{
	cin>>k>>m;
	
	//二分查找x
	
	int l=1,r=1e8;
	while(l<r)
	{
	   int mid=(l+r)>>1;
	   if(check(mid))r=mid;  //如果mid大了,说明x在左半边
	   else l=mid+1;	 //如果mid小了,说明x在右半边
	} 
	cout<<l<<endl;
}
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin >> t;
    while (t--) {
        sovel();
    }
  return 0;	
} 
相关推荐
IT规划师几秒前
数据结构与算法之间有何关系?
数据结构·算法
Xiu Yan22 分钟前
LeetcodeTop100 刷题总结(一)
java·数据结构·算法·链表·矩阵·哈希算法·数组
pyniu29 分钟前
研究生第一次刷力扣day1
算法·leetcode
鱼跃鹰飞34 分钟前
Leetcode面试经典150题-198.打家劫舍
数据结构·算法·leetcode·面试·职场和发展
至简行远1 小时前
路由器全局配置DHCP实验简述
linux·网络·数据结构·python·算法·华为·智能路由器
liuyang-neu1 小时前
力扣 16.最接近的三数之和
java·数据结构·算法·leetcode
碧海蓝天20221 小时前
_Array类,类似于Vector,其实就是_string
数据结构·c++·算法
至简行远1 小时前
路由器接口配置DHCP实验简述
java·服务器·网络·数据结构·python·算法·智能路由器
Antonio9152 小时前
【高级数据结构】并查集
数据结构·c++·算法
肥猪猪爸2 小时前
sklearn特征选取之RFE
数据结构·人工智能·python·算法·机器学习·数据挖掘·sklearn