Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
910 B
0
Indexable
Never
void dfs(int i, int a, int b, int n, string& s, vector<vector<int>>& ans){
	if(i == s.size() || a >= n/2 || a + b > n){
		if(a + b == n)
			ans.push_back({a, b});
		return;
	}
	if(s[i] == '0'){
		dfs(i + 1, a, b, n, s, ans);
		dfs(i + 1, a + (1 << (31 - i)), b + (1 << (31 - i)), n, s, ans);
	}
	else{
		dfs(i + 1, a, b + (1 << (31 - i)), n, s, ans);
		dfs(i + 1, a + (1 << (31 - i)), b, n, s, ans);
	}

}


vector<vector<int>> solve(int n){
	if(n%2 == 1){
		return {{-1, -1}};
	}
	vector<vector<int>> ans;
	vector<vector<int>> temp;
	n = n/2;
	string s = bitset<32>(n).to_string();
	string s2 = bitset<32>(100).to_string();
	string s3 = bitset<32>(44).to_string();
	int i = 0;
	while(i < 32){
		if(s[i] == '1') break;
		i++;
	}
	dfs(i, 0, 0, 2*n, s, ans);
	if(ans.size() == 0) return {{-1, -1}};
	int x = ans.size();
	for(int i = x - 1; i >= 0; i--){
		ans.push_back({ans[i][1], ans[i][0]});
	}
	return ans;
}
Leave a Comment