Last active
July 3, 2016 18:26
-
-
Save joaogui1/93f3c10f59e6bc015b6e5e6d2431656b 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, O(n*2^n) | |
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 O(2^n) | |
int soma = 0; | |
for(int i = 0; i < n; ++i){ //loop O(n) | |
if((1 << i)&mask){ //checa se um elemento está 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