Created
June 26, 2016 15:15
-
-
Save joaogui1/79a9d25978e018bf7d04777b1803f4fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Soma de conjunto | |
int n, v[20], m; | |
cin >> n >> m; | |
for(int i = 0; i < n; ++i) cin >> v[i]; //leitura, O(n) | |
for(int mask = 0; mask < (1 << n); ++mask){ //passa por todos os subconjuntos | |
int soma = 0; | |
for(int i = 0; i < n; ++i) | |
if((1 << i)&mask) //checa se um elemento esta nesse subconjunto | |
soma += v[i]; | |
if(m == soma){ | |
for(int i = 0; i < n; ++i) | |
if((1 << i)&mask) | |
cout << i << " "; | |
cout << "\n"; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment