CodeForces 614A. Link/Cut Tree【坑精度!!!】

来源:互联网 发布:电脑动画制作软件 编辑:程序博客网 时间:2024/04/30 04:26

A. Link/Cut Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 lr 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 lr and k (1 ≤ l ≤ r ≤ 10182 ≤ 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 = 121 = 222 = 423 = 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;}

对最基础的东西还是没有弄清楚,这么简单的问题还是错了这么多次.........


0 0
原创粉丝点击