[关闭]
@xunuo 2017-02-19T13:55:05.000000Z 字数 1546 阅读 953

Codeforces614 A. Link/Cut Tree


time limit per test2 seconds memory limit per test256 megabytes

暴力


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 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.

题意:

一个区间内x的幂有哪些;
输入三个数:r,l,k;区间[l,r]内k的幂;

解题思路:

直接写就可以了,然而我还用了快速幂

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. #define ll long long
  7. ll quick(ll x,ll m)
  8. {
  9. ll ans=1;
  10. while(m>0)
  11. {
  12. if(m%2==1)
  13. ans*=x;
  14. x=x*x;
  15. m=m/2;
  16. }
  17. return ans;
  18. }
  19. int main()
  20. {
  21. ll a,b,k;
  22. while(scanf("%lld%lld%lld",&a,&b,&k)!=EOF)
  23. {
  24. int flag=0;
  25. ll ans;
  26. for(int i=0;;i++)
  27. {
  28. ans=quick(k,i);
  29. if(ans>=a&&ans<=b)
  30. {
  31. flag++;
  32. if(flag==1)
  33. printf("%lld",ans);
  34. else
  35. printf(" %lld",ans);
  36. }
  37. if(b/k<ans)
  38. break;
  39. }
  40. if(flag==0)
  41. printf("-1");
  42. printf("\n");
  43. }
  44. return 0;
  45. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注