B - Link/Cut Tree
Problem 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 exposeprocedure.
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).
Examples
Input
1 10 2
Output
1 2 4 8
Input
2 4 5
Output
-1
Note
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,要求输出在区间内以k为底数的所有指数值。题目比较简单,但有个坑,就是假设当r=10^18时,k=10^9,因为r刚好在题目给定的范围内,所以接下来的t*=k即t=10^9*10^18=10^27结果远远超过unsigned long long最大值(20位数),这将导致数据溢出,因此每次必须判断r除以t得到的倍数是否不小于k,即(r/t)>=k?如果是才可进行相乘(表明相乘之后不大于r),否则break,立即退出,避免数据溢出。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
LL l,r,k,t=;bool flag=false;
cin>>l>>r>>k;
while(t<=r){
if(flag && t<=r)cout<<' '<<t;
if(!flag && t>=l){cout<<t;flag=true;}//先输出第一个数
if((r/t)<k)break;//这一步必须加,不然可能会爆long long,即溢出
t*=k;
}
if(!flag)cout<<"-1"<<endl;
else cout<<endl;
return ;
}
B - Link/Cut Tree的更多相关文章
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
- Link Cut Tree 总结
Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...
随机推荐
- CAD当前选择实体发生变化调用事件(com接口)
主要用到函数说明: _DMxDrawXEvents::SelectModified 当前选择实体发生变化,会调用该事件,详细说明如下: 参数 说明 IDispatch* pAryId 当前被选择的实体 ...
- solaris roles cannot login directly
oracle@solaris:~$ su - root Password: Oracle Corporation SunOS root@solaris:~# cat /etc/user_attr # ...
- NOIp知识点复习——最短路计数
$Mingqi\_H$ NOIp 2017考挂了...gg 重新开始好了. 计划明年2月24号前复习完所有的NOIp知识点(毕竟很不熟练啊),之后到七月底前学习完省选的东西(flag?). 从现在开始 ...
- 小实例 hangman game
代码 #include <bits/stdc++.h> using namespace std; int bk[110]; string sj(int t) { string ans=&q ...
- 二、Scrapy命令行工具
本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/commands.html Scrapy是通过 scrapy 命令行工具 ...
- Codeforces 918D/917B - MADMAX
传送门:http://codeforces.com/contest/918/problem/D 本题是一个组合游戏问题——DAG上的动态规划问题. 有一张有向无环图(DAG).有两个玩家在这张图上进行 ...
- 敏捷开发系列学习总结(2)——Bug修改流程
原则,力求各司其职,简单明了. 1. 测试人员提交bug ⑴ 标题: [ 模块名称 ] 问题描述 ⑵ 内容: 问题重现步骤的描述,最好贴上图片. 因为一图胜万言. ⑶ 指定责任人: 根据bug指定责任 ...
- 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale
https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...
- F1: A Distributed SQL Database That Scales GOOGLE F1 论文
http://research.google.com/pubs/pub41344.html http://research.google.com/pubs/pub36726.html
- DELPHI RTTI实现非可视的功能插件
思路:通过数据字典定义BPL包名,然后定义BPL包里面的类名,然后定义类里面的方法名,最后定义方法的参数值. 可实现动态加载BPL,调用哪个BPL的哪个类的哪个方法并给该方法赋给指定的参数值,如果是函 ...