Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <iostream>#include <cmath>#include <map>using namespace std;int dp(int N, int K, map<pair<int, int>, int>& valMap) {if (N == 0) {return 1;}if (N < 0 || N > (1 << (K + 2)) - 2) {return 0;}auto iter = valMap.find(make_pair(N, K));if (iter != valMap.end()) {return iter->second;}else {int ans = dp(N, K - 1, valMap) + dp(N - (1 << K), K - 1, valMap) + dp(N - (1 << (K + 1)), K - 1, valMap);valMap[make_pair(N, K)] = ans;return ans;}}int main() {int N;cin >> N;int len = log2(N);map<pair<int, int>, int> valMap;cout << dp(N, len, valMap) << '\n';return 0;}