@lychee123
2017-02-21T08:56:51.000000Z
字数 631
阅读 1363
水题
题意
The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 10^18, 2 ≤ k ≤ 10^9).
Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).
输出 在l 到r 区间内(包含l,r)是k的幂的数
分析
这是一个水题,但是有一个细节需要注意,在用ans * k的时候容易爆long long所以要有一个判断如果r/ans < k就必须跳出,r不会爆,但是ans * k的值可能会超过r
#include<stdio.h>#include<algorithm>using namespace std;int main(){long long l,r,i,k,ans=1,flag=0;scanf("%lld%lld%lld",&l,&r,&k);for(i=0;;i++){if((ans>=l)&&(ans<=r)){printf("%lld ",ans);flag=1;}if(k>r/ans)///防止爆long longbreak;ans*=k;if(ans>r)break;}if(flag==0)printf("-1");printf("\n");return 0;}
