Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- void backtracking (int n, int comb, bool& primer, const vector<int> & mult, priority_queue <int, vector<int>, greater<int> >& Q)
- {
- if (n == 0) Q.push(comb);
- else {
- int i = 0;
- if (primer) {
- i = 1;
- primer = false;
- }
- for (; i <= 9; ++i) {
- int aux = comb*10 + i;
- bool trobat = false;
- int j = 0;
- while (not trobat and j < (int)mult.size()) {
- trobat = aux%mult[j] == 0;
- ++j;
- }
- if (not trobat) backtracking (n-1, aux, primer, mult, Q);
- }
- }
- }
- int main () {
- int n, m;
- while (cin >> n >> m) {
- //vector<int> comb (n);
- priority_queue <int, vector<int>, greater<int> > Q;
- //vector<bool> marca (n);
- vector<int> mult (m);
- for (int i = 0; i < m; ++i) {
- //enters entre 2 i 1000
- cin >> mult[i];
- }
- bool primer = true;
- backtracking(n, 0, primer, mult, Q);
- while (not Q.empty()) {
- cout << Q.top() << endl;
- Q.pop();
- }
- cout << "----------" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement