WA 70 求调

k的幂次

题目ID:1238必做题100分

最新提交:

Wrong Answer

70 分

历史最高:

Wrong Answer

70 分

时间限制: 1000ms

空间限制: 32768kB

题目描述

k的0次方,1次方,2次方这些都叫做k的幂。

找出l和r之间k的幂,如果没有则输出-1。

输入格式

三个整数 l,r,k

输出格式

[l,r]之间k的所有幂

样例

Input 1

1 10 2

Output 1

1 2 4 8

Input 2

237171123124584251 923523399718980912 7150

Output 2

-1

样例解释

样本输入1:
1 10 2

样本输出1: 1 2 4 8

样本输入2:
237171123124584251 923523399718980912 7150

样本输出2: -1

数据范围

1 <= l <= r <= 10^18,2 <= k <= 10^9

1<=l<=r<=10^18,2<=k<=10^9

#include<bits/stdc++.h>
#define int long long
using namespace std;

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	
	int l,r,k;
	cin>>l>>r>>k;
	
	bool flag=0;
	int ans=1;
	
	while(1)
	{
		if(ans>r||ans<=0)
		{
			break;
		}

		if(ans>=l&&ans<=r)
		{
			cout<<ans<<" ";
            flag=1;
			ans*=k;
		}
		
		if(ans<l&&ans>=1)
		{
			ans*=k;
		}

	}

    if(!flag)
    {
        cout<<-1;
    }
  
	return 0;
}

?你不是提高的吗,怎么会问这个问题?被jc了?

1 个赞

?不是提高

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll l,r,k,flag,kk=1;
int main()
{
	cin>>l>>r>>k;
    if(l<=1&&r>=1) cout<<1<<" ";
	for(ll i=0;;i++){
        kk*=k;
		if(kk>=l&&kk<=r){
			cout<<kk<<" ";
			flag=1;
		}
        if(r/k<kk) break;
	}
	if(!flag) cout<<-1;
}

你这样会超限的,虽然我只有WA60过,这个AC代码给你看看

因该要用快速幂,记得开longlong

谢谢,不过我已经AC了

这样会TLE