CodeForces 614 A. Link/Cut Tree(水~)

Description
给出一个区间[l,r]和一整数k,升序输出区间[l,r]中所有满足k^t的数,如果不存在这样的数则输出-1
Input
三个整数l,r,k(1<=l<=r<=10^18,2<=k<=10^9)
Output
升序输出区间[l,r]中所有满足k^t的数
Sample Input
1 10 2
Sample Output
1 2 4 8
Solution
水题,注意乘法可能会爆long long,所以每次判断与r的关系时要用除法
Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int main()
{
    ll l,r,k;
    while(~scanf("%I64d%I64d%I64d",&l,&r,&k))
    {
        ll ans=1ll;int flag=0;
        while(ans<=r)
        {
            if(ans>=l)
                flag=1,printf("%I64d ",ans);
            if(ans>r/k)break;
            ans*=k;
        }
        if(!flag)printf("-1");
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(CodeForces 614 A. Link/Cut Tree(水~))