longlong binom[64][64]; longlongnCr(longlong n, longlong r){ if (r < 0 || r > n || n >= 64) return0; if (r == 0 || r == n) return1; if (binom[n][r] != 0) return binom[n][r]; return binom[n][r] = nCr(n-1, r-1) + nCr(n-1, r); } classSolution { intcountDepth(int numberOfBits){ int res = 1; while(numberOfBits > 1) { numberOfBits = __builtin_popcount(numberOfBits); res++; } return res; } longlonghelper(longlong n, vector<int>& bits, longlong usedBits, longlong shift, bool less){ if(shift == -1) { // check n is k-popcountDepth for(auto& bit : bits) if(bit == usedBits) return1; returnfalse; } if(less) { // case if current built value is less then n longlong res = 0; for(auto& bit : bits) { if(bit < usedBits) continue; res += nCr(shift + 1, bit - usedBits); } return res; } if((n & (1ll<<shift)) == 0) { // case if n's shift_th bit is off returnhelper(n,bits,usedBits,shift - 1, false); } // case if n's shift_th bit is on longlong res = helper(n,bits,usedBits, shift - 1, true); res += helper(n, bits, usedBits + 1, shift - 1, false); return res; } public: longlongpopcountDepth(longlong n, int k){ if(k == 0) return1; vector<int> depth(64); for(int numberOfBits = 1; numberOfBits < 64; numberOfBits++) { depth[numberOfBits] = countDepth(numberOfBits); } vector<int> bits; for(int i = 1; i < 64; i++) if(depth[i] == k) bits.push_back(i); longlong res = helper(n,bits,0,63,false); // cause 1 is not 1 popcountDepth if(k == 1) res--; return res; } };