614A

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 rinclusive. 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 ≤ 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).

Sample test(s)
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^0,k^1,k^2这些数字中处于该区间的数,并输出这些数

错因分析:看到题目第一反应就是想到了快速幂,然后迅速的写了上去,结果后来才意识到

快速幂会犯一个比较麻烦的的错误;直接将数相乘就好了嘛,干嘛要那么麻烦的快速幂,定势思维了

下面第一个是AC代码

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int main()
{
    long long l,r,v;
    while(~scanf("%lld %lld %lld",&l,&r,&v))
    {
        int flag=0;long long ans=1;
        while(ans<=r)
        {
            if(ans>=l)
            {
                flag=1;
                printf("%lld ",ans);
            }
            if(r/v>=ans)/*写成乘的形式是可能会爆long long的
                 因此是错误的额*/
          ans*=v; else break; } if(!flag) printf("-1"); printf(" "); } return 0; }

  下面是wa的代码注意WA原因是不太好避免上面指出的那个错误:

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
long long cimi(long long a,long long p)
{
    long long res=1,temp=a;
    while(p)
    {
        if(p&1)
           res*=temp;
        temp*=temp;
        p>>=1;
    }
    return res;
}
int main()
{
    long long l,r,v;
    while(~scanf("%lld %lld %lld",&l,&r,&v))
    {
        int flag=0;long long ans=0;
        for(long long  i=0;;i++)
        {
            ans=cimi(v,i);
           if(ans>=l&&ans<=r)
               {
                    printf("%lld ",ans);
                    flag=1;
               }
            else if(ans>r)
                break;
        }
        if(!flag)
            printf("-1");
        printf("
");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/smilesundream/p/5133159.html