CodeForces614A Link/Cut Tree(爆longlong处理)

J - Link/Cut Tree
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 614A
Description
Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure.

Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn’t, then why would he need Splay trees anyway?)

Given integers l, r and k, you need to print all powers of number k within range from l to rinclusive. However, Rostislav doesn’t want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

Input
The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).

Output
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).

Sample Input
Input
1 10 2
Output
1 2 4 8
Input
2 4 5
Output
-1
Hint
Note to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn’t be printed.

题意:求在[L R]之间 所有k的次方。l,r≤1e18 2≤k≤1e9
分析:因为是次方,枚举次方代价很小即使k为2 最多也就几十次,所以可以枚举,判断该数是否在范围内。
困难: 判断k ≤r 的时候会爆。
举个例子:k=1000 l=1;r=1e18; tot=1e18是它的次方。然后接着乘次方到1e21 再发现爆了。可1e21本身连 ULL 都存不下。所以要换一种判断方式 此时如果 r/tot

// Created by ZYD in 2015.
// Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define EPS 1e-8
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
int n,m,a,x;
unsigned long long l,r,k;
int main()
{
    //freopen("in.txt","r",stdin);

    while(~scanf("%lld%lld%lld",&l,&r,&k)){
    unsigned long long tot=1;
    int flag=0;
    while(tot<=r){
        if(l<=tot) {printf("%lld ",tot); flag=1;}
         if((r/tot)<k) break; //cout<<r/tot<<"*";
        tot*=k;
         // cout<<tot<<endl;
    }
    //printf("%lld\n",r);
    if(flag==0) printf("-1");
    printf("\n");
    }
    return 0;
}

你可能感兴趣的:(codeforces)