蓝桥杯小白赛第六期 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;	
} 
相关推荐
.柒宇.4 小时前
力扣hot100----15.三数之和(java版)
java·数据结构·算法·leetcode
杰克尼5 小时前
二分查找为什么总是写错
java·数据结构·算法
程序员阿鹏8 小时前
56.合并区间
java·数据结构·算法·leetcode
古译汉书9 小时前
Stm32江科大入门教程--各章节详细笔记---查阅传送门
数据结构·stm32·单片机·嵌入式硬件·算法
Jasmine_llq11 小时前
《P2656 采蘑菇》
算法·强连通分量·广度优先搜索(bfs)·tarjan 算法·图的缩点操作·有向无环图(dag)·最长路径
芥子沫11 小时前
《人工智能基础》[算法篇3]:决策树
人工智能·算法·决策树
mit6.82411 小时前
dfs|位运算
算法
苏纪云11 小时前
算法<C++>——双指针 | 滑动窗口
数据结构·c++·算法·双指针·滑动窗口
保持低旋律节奏11 小时前
算法——二叉树、dfs、bfs、适配器、队列练习
算法·深度优先·宽度优先