题意:
如果一个数字是Good Number,当且仅当 ⌊ x k ⌋ \left \lfloor\sqrt[k]{x}\right \rfloor ⌊kx⌋(向下取整) 能整除 x 。
现在给出 n,k ,求 1 到 n 之中Good Number 的个数。
题目:
Alex loves numbers.
Alex thinks that a positive integer x is good if and only if ⌊ x k ⌋ \left \lfloor\sqrt[k]{x}\right \rfloor ⌊kx⌋ divides x.
Can you tell him the number of good positive integers which do not exceed n ?
Input
The first line of the input gives the number of test cases, T (1≤T≤10). T test cases follow.
For each test case, the only line contains two integers n and k (1≤n,k≤ 1 0 9 10^{9} 109).
Output
For each test cases, output one line containing “Case #x: y”, where x is the test case number (starting from 1), and y is the answer.
Example
Input
2
233 1
233 2
Output
Case #1: 233
Case #2: 43
分析:
1.特判 首先对于 m=1,m>32 判断一下,如果成立直接输出 n ,因为
2
32
2^{32}
232>109 ,开根号之后只能是 1 。
2.之后遍历(1~n)的m次方数,接下来操作举例演示:
例如 m=2 ,n=20 ,用 arr 记录 x ,用 brr 记录 xk 。
对于 1~3 之间的数字开根号向下取整为1,故1~3之间的数字都是Good Number。4~8之间的数字开根号都为2,有 8−4=4 个数字(分别是 5,6,7,8) ,其中有2个数字能被2整除,而 4 本身也能被 2 整除。所以对于4~8区间内的Good Number个数为 (8−4)/2+1。同理 9~15 之间的数字开根号都为 3 ,Good Number 个数为 (15−9)/3+1。对于最后的 16~20 ,开根号都为4,所以最后加上 (n−16)/4+1 。
AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int t,k;
ll n,m,ans;
int main(){
scanf("%d",&t);
k=0;
while(t--){
scanf("%lld%lld",&n,&m);
printf("Case #%d: ",++k);
if(m==1)
printf("%lld\n",n);
else{
ans=0;
for(ll i=1;i<=n;i++){
ll x=i,y=i+1;
if(i==1){
for(int j=1;j<m;j++){
y=y*(i+1);
if(y>n) break;
}
}
else{
for(int j=1;j<m;j++){
x=x*i;
y=y*(i+1);
if(x>n) break;
}
}
if(x>n) break;
x=x-1;
y=min(n,y-1);
ll tep=y/i-x/i;
ans+=y/i-x/i;
}
printf("%lld\n",ans);
}
}
return 0;
}