Codeforces - 614A (爆long long 处理)
题目链接:http://codeforces.com/contest/614/problem/A
题意:求在[L R]之间 所有k的次方。l,r≤1e18 2≤k≤1e9
分析:因为是次方,枚举次方代价很小即使k为2 最多也就几十次,所以可以枚举,判断该数是否在范围内。
困难: 判断k ≤r 的时候会爆。
举个例子:k=1000 l=1;r=1e18; tot=1e18是它的次方。然后接着乘次方到1e21 再发现爆了。可1e21本身连 ULL 都存不下。所以要换一种判断方式 此时如果 r/tot
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll l,r,k;
cin >> l >> r >> k;
int isp = 0;
ll ans = 1;
while ( 1 ) {
if ( ans>=l && ans<=r ) {
cout << ans << " ";
isp = 1;
}
if ( r/ans<k ) { // 即判断r<ans*k,
break ; // 因为ans*k可能越界,所有用除法判断
}
ans *= k;
}
if ( isp==0 ) {
cout << "-1" <<endl;
}
else {
cout << endl;
}
return 0;
}