1, 2, 4, 7, 8, 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44, 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81, 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112, 115, 117, 118, 121, 122, 124,....
规律题
通过打表易得上述序列,然后可以发现,其第n项的值就是(n-1)*2+(hammingweight(n-1)+1)%2;
hammingweight指的是对应数字的二进制下有多少个1;
直接根据公式求解即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=505;
const int mod=1000000007;
int T;
ll n,m;
int func(ll n)
{
int cou=0;
while(n>0)
{
n=n&(n-1);
cou++;
}
return cou;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>T;
while(T--)
{
cin>>n;
n--;
cout<<n*2+(func(n)+1)%2<<endl;
}
return 0;
}