二分走一波,没想到题解的大佬做法 p_q
注意爆long long,所以先对数取一下上限
二分确定下限,然后输出
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include <limits.h>
#include<algorithm>
#define MAXN 1010100
#define LL unsigned long long
// #define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b){LL ans=1;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
LL ll,rr,w,L,R,cnt;
LL mid,ans;
int main(){
scanf("%llu%llu%llu",&ll,&rr,&w);
if(w==1){
puts("1");
return 0;
}
int l=0,r=log(rr)/log(w);
R=r;
l=0,r=log(rr)/log(w);
ans=-1;
while(l<=r){
mid=(l+r)/2;
if(powmod(w,mid)>=ll)ans=mid,r=mid-1;
else l=mid+1;
}
L=ans;
for(int i=L;i<=R;i++){
ans=powmod(w,i);
if(ans>=ll&&ans<=rr)
printf("%lld ",ans),cnt++;
}
if(!cnt)puts("-1");
return 0;
}