一道找规律的题
打一个暴力代码,可以发现:
| n | ans |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 2 |
| 4 | 4 |
| 5 | 7 |
| 6 | 7 |
| 7 | 7 |
| 8 | 8 |
| 9 | 15 |
| 10 | 15 |
| 11 | 15 |
| 12 | 15 |
| 13 | 15 |
| 14 | 15 |
| 15 | 15 |
| 16 | 16 |
前 个是特殊的
时,
时,,即 。且最小的 的幂
时间复杂度 , 是寻找幂的时间
#include <bits/stdc++.h>
using namespace std;
long long is_power(long long n)
{
long long power = 1;
for(int j = 0; j <= 62; j++)
{
if(n <= power) return power;
power *= 2;
}
return -1;
}
void solve()
{
long long n;
scanf("%lld",&n);
if(n == 1){printf("1\n"); return;}
if(n == 2){printf("3\n"); return;}
if(n == 3){printf("2\n"); return;}
if(is_power(n) == n)
{
printf("%lld\n",n);
return;
}
if(is_power(n + 1) == n + 1)
{
printf("%lld\n",n);
return;
}
printf("%lld\n",is_power(n) - 1);
}
signed main()
{
int T; scanf("%d",&T);
while(T--) solve();
}