Link/Cut Tree
2016-02-26 16:40
429 查看
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
Output
Input
Output
Sample Output
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.
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
Sample Output
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.
#include<algorithm> #include<cstdio> #include<cmath> #include<cstring> #include <iostream> #include<cstdlib> using namespace std; int main() { long long l, r, k, i, x, y = 0; scanf("%lld%lld%lld", &l, &r, &k); x = 1; for (i = 0;x<=r;) { if (x >= l) { printf("%lld ", x); y = 1; } if (x > r/k) break; x *= k; } if (y == 0) { printf("-1\n"); } return 0; }
相关文章推荐
- Simple Game
- SSM-如何验证事务是否生效
- mybaits之动态sql
- scala note
- 单链表的创建/测长/打印
- Material Design Theme
- 获取私有变量名称
- linux vi 删除多行的方法
- 【BZOJ2741】FOTILE模拟赛 L
- ios 资源很全面转载以后方便看
- docker 升级版本
- 【hdu5015】233 Matrix——矩阵快速幂
- oracle中使用物化视图实现表的同步
- Mac OS X 下开启PHP
- Adroid: getExternalStorageDirectory 不一定是你想要的外部存储SdCard
- Spring Aop自定义注解实现日志管理
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 从数组谈队列
- YII url美化管理
- LAoj 2678 - Subsequence (DP&技巧&排序)