春测2023 T2题解
这是一个从暴力到正解的过程。
暴力#
从
改善1#
此时我们抛开复杂度的
故如果
改善2#
试着不用map数组判重,可以发现,重复时会有
如果
这样在枚举底数时,如果自己已经被
上代码#
#include<bits/stdc++.h>
#define int __int128
using namespace std;
bool vis[2000000];
int ll=0;
inline __int128 read(){
__int128 x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void print(__int128 x){
if(x<0){putchar('-');x=-x;}
if(x>9)print(x/10);
putchar(x%10+'0');
}
signed main(){
// freopen("power.in","r",stdin);
// freopen("power.out","w",stdout);
long long n,k;
cin>>n>>k;
if(k==1){
print(n);return 0;
}
if(k>log2(n)){
cout<<1;return 0;
}
int ans=1;
int b=0;
for(int a=2;a<=1000000;a++){
int res=a;b=1;
if(vis[res])continue;
while(1){
b++;
res=res*a;
if(b>=k&&res<=n){
if(k==2&&res<=n&&b%2==0){//只有k=2时才不加
if(res<=1000000)vis[res]=1;//标记
continue;//不加答案
}
ans++;
}
if(res<=1000000)vis[res]=1;//注意不管符不符合答案就会标记上
if(res>=n)break;
}
}
if(k==2)ans+=(int)sqrtl(n)-1;//因为ans初值为1,表示1也符合答案,所以在这里sqrtl(n)也包含了1,需减去
print(ans);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?