According to @templatetypedef 's core algorithm, I finally got it Accepted:
# Problem Verdict Language Run Time Submission Date
12534054 10232 Bit-wise Sequence Accepted C++ 0.018 2013-10-21 00:39:12
The credit should still go to @templatetypedef, but I'm also posting the main code for others' reference.
The code is actually short, because most is my comments :-)
The main code (I spent a lot of time solving the "Offset 1" issue):
#include <cstdio>
#include <bitset>
#include <algorithm>
#define N 32
using namespace std;
unsigned C[N][N]; // C[k][n] means choose k objects from n objects
unsigned S[N];
void CreateLookupTable()
{
// Create the C(k, n)
for (int n = 0; n < N; ++n)
C[0][n] = 1;
for (int n = 1; n < N; ++n)
{
for (int k = 1; k < n; ++k)
C[k][n] = C[k][n-1] + C[k-1][n-1];
C[n][n] = 1;
}
// Construct an accumulated sequence from C[i, 31], where i is [0..31]
int n = N-1;
S[0] = C[0][n];
for (int i = 1; i < N; ++i)
S[i] = S[i-1]+C[i][n];
}
// n: the position in the current bucket
// b: how many 1's in the target number
// h: the index of the possible highest 1
inline void FillBitset(int n, int b, int h, bitset<N>& bs)
{
if (b == 0)
return;
// Search for which small bucket the number is. Similar as before,
// If b = 4, h_ = 3, there will be C[3][3] numbers
// If b = 4, h_ = 4, there will be C[3][4] numbers
// If b = 4, h_ = 5, there will be C[3][5] numbers
// ...
// If b = 4, h_ = h, there will be C[3][h] numbers
//
// Also, it's very easy to prove that:
// C[3][3] = C[4][4]
// C[3][3] + C[3][4] = C[4][5]
// C[3][3] + C[3][4] + C[3][5] = C[4][6]
// ...
// C[3][3] + C[3][4] + C[3][5] + .. + C[3][h] = C[4][h+1]
//
// Now let's determine which C[b][i], where i is [b..(h+1)]
unsigned* lb = lower_bound(&C[b][b], &C[b][h+1]+1, n);
int c = lb-(&C[b][b])+b;
// Don't forget to decrease c to get the index of the highest bit 1
--c;
// Fill the actual highest bit
bs.set(c);
// When c < b, lb must point to C[b][b] == 1, which means all lower bits
// are also 1's, and we should stop here
if (c < b)
{
for (int i = 0; i < c; ++i)
bs.set(i);
return;
}
// Deduct the number of numbers in the lower buckets, and search for
// (b-1) 1's in a smaller bucket. Apparently, the possible highest 1
// should be at index c-1, since the current bucket's highet 1 is at
// index c
FillBitset(n-C[b][c], b-1, c-1, bs);
}
inline unsigned GetNumber(int n)
{
if (n == 0)
return 0;
// From index to position
++n;
// Get which bucket Number[n] is
unsigned* lb = lower_bound(S, S+N, n);
int b = lb-S; // There are b 1's in the number, and b is always > 0
// because n == 0 is excluded above
// So let's go to the core algorithm: get the position in the bucket
// For each bucet, we need to divide it into small bags/buckets.
//
// If b = 4, there will be [3..30] = 28 bags to represent how many numbers
// whose highest one is at index 3, 4, 5, .. 30
//
// If b = 4, and the highest 1 is at index 3, there will be C[3][3] numbers
// If b = 4, and the highest 1 is at index 4, there will be C[3][4] numbers
// If b = 4, and the highest 1 is at index 5, there will be C[3][5] numbers
// ...
// If b = 4, and the highest 1 is at index 30, there will be C[3][30] numbers
//
// Now our task is to find which small bucket the number is
bitset<N> bs;
FillBitset(n-S[b-1], b, N-2, bs);
return (unsigned)bs.to_ulong();
}
Test Input:
0
1
2
31
32
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
496
497
498
2147483647
1234567890
987654321
6123512
852412
123125
67658153
214155
5623674
Test Output:
0
1
2
1073741824
3
0
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
3
5
1610612736
7
11
2147483647
1195924317
1467508257
147227152
1099186184
135856144
1247429124
57624
100730919