P10320 勇气(Courage) solution
Description
给定
求一个最小的
Solution
这里直接分析满分做法。
看数据范围,
再回来看函数
所以我们容易发现规律:在上面每一项中,分母的指数为
但很显然,这个值非常大,和
所以,我们可以按照酱紫的思路,通过对
- 对于
2^n ,一定:
- 对于
f(k) :
这里用到了对数函数的一些性质,不会的请自行百度,这里就不再赘述了。
而因为
然后就是无解的情况。无解的情况当且仅当
接下来就是分类讨论
-
当
x=1 时:\ 即使n 取最小值1 ,也有2^n=2 ,无解,所以x=1 时一定无解。 -
当
x=2 时:- 如果
n=1 ,那么2^n=2 ,此时x 已经满足要求了,于是有解,且答案是0 。 - 如果
n=2 ,那么2^n=4 ,此时在进行1 次操作后,f(1)=x^2=4 ,满足要求,有解,答案是1 。 - 否则,
f(k) 的最大值就是4 ,于是无解。
- 如果
然后注意
Code
#include<iostream>
#include<cstdio>
#include<cmath>
#define ll long long
#define il inline
using namespace std;
ll x,n;
double lg;
il ll fpow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans*=a;
b>>=1;
a*=a;
}
return ans;
}
il double check(ll k)
{
double now=fpow(2,k)*(lg-1)+2;
return now;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>x>>n;
if(x==1)
{
cout<<"inf";
return 0;
}
if(x==2)
{
if(n==1) cout<<0;
else if(n==2) cout<<1;
else cout<<"inf";
return 0;
}
lg=log2(x);
ll ck=0;
while(check(ck)<n)
++ck;
cout<<ck;
return 0;
}