A. Link/Cut Tree

题目链接:http://codeforces.com/problemset/problem/614/A

 

题意:

给你一个范围[l,r],求k的i次方在那个范围的数

 

思路:

这题的坑点就在于long long 可能会发生溢出,如果溢出的话我们就停止

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdlib.h>
 4 #include <cstring>
 5 #include <string>
 6 #include <string.h>
 7 #include <set>
 8 #include <queue>
 9 #include <stdbool.h>
10  
11 #define LL long long
12 using namespace std;
13 const int maxn = 1e5 + 10;
14  
15 LL L,R;
16 LL k;
17 LL arr[maxn];
18  
19 int main(){
20     cin >> L >> R >> k;
21     LL cnt = 0;
22     LL ans = 1;
23     while (1){
24         if(ans>=L && ans<=R){
25             arr[cnt++] = ans;
26         }
27         if (R/ans < k || ans >= R) // 如果ans * k 会发生溢出所以改除法
28             break;
29         ans *= k;
30     }
31     if (cnt == 0){
32         printf("-1\n");
33     }
34     else{
35         for (int i=0;i<cnt;i++){
36             printf("%lld ",arr[i]);
37         }
38     }
39     return 0;
40 }

 

posted @ 2019-08-18 00:29  _Ackerman  阅读(211)  评论(0编辑  收藏  举报