#include <iostream>
using namespace std;
const int maxn = 1000;
 
struct stackk {
    int niza[maxn];
    int idx;
    
    void init() {
        idx = -1;
    }
    
    void push(int x) {
        if(idx + 1 >= maxn) {
            cout << "NEMA DOVOLNO KAPACITET" << endl;
            return;
        }
        idx++;
        niza[idx] = x;
    }
    
    int pop() {
        if(idx == -1) {
            cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
            return -1;
        }
        int result = niza[idx];
        idx--;
        return result;
    }
    
    int top() {
        if(idx == -1) {
            cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
            return -1;
        }
        return niza[idx];
    }
    
    int size() {
        return idx + 1;
    }
    
    int isEmpty() {
        if(idx == -1) {
            return 1;
        }
        else {
            return 0;
        }
    }
};

void promeni(stackk & m1) {
    stackk m2;
    m2.init();
    
    while(m1.isEmpty() == false) {
        m2.push(m1.pop());
    }
    
    stackk nad;
    nad.init();
    
    while(m2.isEmpty() == false) {
        int c = m2.pop();
        
        if(nad.isEmpty() == false) {
            if(c % nad.top() == 0) {
                m1.push(c / nad.top());
            }
        }
        nad.push(c);
        
        if(m2.isEmpty() == false) {
            if(c % m2.top() == 0) {
                m1.push(c / m2.top());
            }
        }
    }
}
int main() {
    stackk m1;

    m1.init();

    m1.push(2);
    m1.push(10);
    m1.push(5);
    m1.push(2);
    m1.push(1);

    promeni(m1);

    while (!m1.isEmpty()) {
        cout << m1.pop() << endl;
    }

    return 0;
}
