CODEFORCES #339 div2 A
2016-01-15 21:33
309 查看
A. Link/Cut Tree
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 r inclusive. 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).
题目简单来说就是输出在区间[l,r]上所有k^x。水题,但是要留意l,r的最大值可为10^18,k的最大值可达到10^9若不留意比较方法可能会造成数据溢出而导致结果出错。
所以我在做的时候决定用tem存储当前可能会输出的数,将r/tem与k比较以此来决定是否输出结束,可以防止数据溢出。
代码:
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 r inclusive. 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).
题目简单来说就是输出在区间[l,r]上所有k^x。水题,但是要留意l,r的最大值可为10^18,k的最大值可达到10^9若不留意比较方法可能会造成数据溢出而导致结果出错。
所以我在做的时候决定用tem存储当前可能会输出的数,将r/tem与k比较以此来决定是否输出结束,可以防止数据溢出。
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { long long l,r,k; while (scanf("%I64d%I64d%I64d",&l,&r,&k)==3) { long long tem=1; int cnt=0; if (k==1) { if (l<=1&&r>=1) printf("%I64d\n",k); else printf("-1\n"); } else { if (tem<l) { while (l/tem>=k) tem*=k; } if (tem<l&&r/tem<k) printf("-1\n"); else { if (tem<l) tem*=k; printf("%I64d",tem); while (r/tem>=k) { tem*=k; printf(" %I64d",tem); } printf("\n"); } } } return 0; }
相关文章推荐
- transform算法
- Ansi、Unicode、UTF8字符串之间的转换和写入文本
- office2010安装提示需要MSXML版本6.10.1129.0
- 深入交流,发现更多--总结米老师语录
- 一位小白的初来乍到。
- 自己的半年学习总结--发文于2012-3-30
- 从模块化的角度看待CNN网络
- Microsoft Visual Studio (VS)2010 常用快捷键大全 便捷开发
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
- Tachyon源码结构分析(一)
- 简单计时器
- 第一章 面向对象编程概述
- POJ 1502 MPI Maelstrom(单源最短路)
- 分享多目标蝙蝠优化算法MATLAB程序
- Android笔记--对Activity、View、Window的理解
- 我的Visual Studio 2013常用快捷键
- 山寨风,高仿QQ附近的人筛选功能的滑动选择列表来袭!
- iOS 添加字体库 —— HERO博客
- 界面之下:还原真实的MV*模式 (MVC, MVP, MVVM模式)
- C++ STL 队列入门