CodeForces 614A. Link/Cut Tree【坑精度!!!】
来源:互联网 发布:电脑动画制作软件 编辑:程序博客网 时间:2024/04/30 04:26
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!
The first line of the input contains three space-separated integers l, r and k (1 ≤ l ≤ r ≤ 1018, 2 ≤ k ≤ 109).
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).
1 10 2
1 2 4 8
2 4 5
-1
Note to the first sample: numbers 20 = 1, 21 = 2, 22 = 4, 23 = 8 lie within the specified range. The number24 = 16 is greater then 10, thus it shouldn't be printed.
题意:
给出一个区间范围,和一个k 值,输出区间范围内k 的幂
题解:
错了n次,主要是溢出问题
两种思路解决:
1,计算出两端的边界情况,然后无脑累乘输出
注意log 的精度,double 和 long long 的转化精度是不够的,需要额外逼近一下
/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<cmath>typedef long long ll;const double eps=1e-6;double log(ll n,ll m){return log(n*1.0)/log(m*1.0);}ll power(ll n,ll m)//快速幂{ll ans=1;while(m){if(m&1){ans*=n;}n*=n;m>>=1;}return ans;}void slove(ll l,ll r,ll k){ll bg=log(l,k)+eps,ed=log(r,k)+eps;//大约值while(power(k,bg)<l)//尝试逼近一下{++bg;}while(power(k,ed)>r)//同上{--ed;}if(bg>ed){printf("-1\n");return;}ll ans=power(k,bg);printf("%I64d",ans);for(ll i=bg+1;i<=ed;++i){ ans*=k; printf(" %I64d",ans);}printf("\n");}int main(){ll l,r,k;//freopen("shuju.txt","r",stdin);while(~scanf("%I64d%I64d%I64d",&l,&r,&k)){slove(l,r,k);}return 0;}
2,累乘会超出范围,导致输出出错,那么就可以处理成除的情况
/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<cmath>typedef long long ll;void slove(ll l,ll r,ll k){ll ans=1,kase=0,cnt=0;while(1){if(ans>=l&&ans<=r){kase=1;if(cnt++){printf(" ");}printf("%I64d",ans);}if(ans>r/k) {break;}ans*=k;}if(!kase){printf("-1\n");}printf("\n");}int main(){ll l,r,k;//freopen("shuju.txt","r",stdin);while(~scanf("%I64d%I64d%I64d",&l,&r,&k)){slove(l,r,k);}return 0;}
对最基础的东西还是没有弄清楚,这么简单的问题还是错了这么多次.........
- CodeForces 614A. Link/Cut Tree【坑精度!!!】
- Codeforces 614 A Link/Cut Tree
- codeforces 614A Link/Cut Tree
- CodeForces 614A Link/Cut Tree
- codeforces 614A Link/Cut Tree
- codeforces 614 A. Link/Cut Tree
- 【CodeForces】[614A]Link/Cut Tree
- CodeForces 614 A. Link/Cut Tree(水~)
- Codeforces--614A--Link/Cut Tree(数学)(暴力求解)
- Codeforces-----614A---Link/Cut Tree---数学水题
- 614A Link/Cut Tree
- Link/Cut Tree CodeForces
- A. Link/Cut Tree
- Codeforce 614A Link/Cut Tree 水题
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree
- Codeforces Round #339 (Div. 2) A.Link/Cut Tree
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree
- Codeforce 题目614AA. Link/Cut Tree(水题,坑精度啊)
- 小小ARC造福无数码农
- bootstrapTable
- Xen-Server 6.5 配置 License Management 授权
- 安装fftw
- 一个简单的C语言的汇编程序
- CodeForces 614A. Link/Cut Tree【坑精度!!!】
- Android开发之非Activity类型的Context启动一个目标Activity
- 《剑指Offer》学习笔记——数值的整数次方
- nyoj 73比大小
- Nginx性能优化----系统篇
- CSS利用padding和margin正负相消实现多列等高
- Mysql的数据库的备份和还原
- TCP、UDP、三次握手、四次挥手
- J友