[春季测试 2023] 幂次

· · 题解

题意:给定 n,k(n\le1e18,1\le k\le100),求多少 x 可被表示为 x=a^b\le n 且 b\ge k 的个数。

首先发现 k=1 时,答案为 n。

大于 $1e6$ 的部分只会贡献平方,对于 $k=2$ 的处理可以先加上 $\sqrt{n}$ ,在枚举时只选择不是平方数的加上,最后注意 $sqrt$ 精度不够,需使用 $sqrtl$。 ```cpp line-numbers #include <bits/stdc++.h> using namespace std; map <long long,bool> f; int main() { long long n; int k; scanf("%lld%d",&n,&k); if(k==1) { printf("%lld\n",n); return 0; } long long ans=1; if(k==2) ans=sqrtl(n); for(int i=2;i<=1e6;i++) { long long x=i; if(n/x<i) continue; for(int j=2;j<=60;j++) { x*=i; if(k==2&&(long long)sqrtl(x)*sqrtl(x)!=x&&!f[x]) { ans++; f[x]=1; } if(k>2&&j>=k&&!f[x]) { ans++; f[x]=1; } if(n/x<i) break; } } printf("%lld\n",ans); return 0; } ```