1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <iostream>
#include <vector>

using namespace std; 

long long result = 0;
vector<long long> kaczki;

void licz_rek(long long h, long long w, long long hi, long long wj) {
    if (h <= 0 || w <= 0) return;
    if (hi <= 0) hi=0;
    if (wj <= 0) wj=0;

    long long ilosc_h, ilosc_w;
    while (hi>=0 && h/kaczki[hi] == 0) {
        hi--;
    }
    /*while (wj>=0 && w/kaczki[wj] == 0) {
        wj--;
    }*/
    while (hi>=0 && w/kaczki[hi] == 0) {
        hi--;
    }

    if (hi<0) {
        hi = 0;
    }
    /*if (wj<0) {
        wj = 0;
    }*/
    ilosc_h = h/kaczki[hi];
    //ilosc_w = w/kaczki[wj];
    ilosc_w = w/kaczki[hi];
    result += ilosc_h*ilosc_w;

    licz_rek(h-kaczki[hi]*ilosc_h, kaczki[hi]*ilosc_w, hi, hi);
    licz_rek(kaczki[hi]*ilosc_h, w-kaczki[hi]*ilosc_w, hi, hi-1);
    licz_rek(h-kaczki[hi]*ilosc_h, w-kaczki[hi]*ilosc_w, hi-1, hi-1);
    //licz_rek(h-kaczki[hi]*ilosc_h, kaczki[wj]*ilosc_w, hi-1, wj);
    //licz_rek(kaczki[hi]*ilosc_h, w-kaczki[wj]*ilosc_w, hi, wj-1);
    //licz_rek(h-kaczki[hi]*ilosc_h, w-kaczki[wj]*ilosc_w, hi-1, wj-1);

}

int main(long long argc, char *argv[])
{

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    long long h, w;
    long long n;
    long long koczkodany;
    

    cin >> h >> w >> n;

    for (long long i=0; i<n; i++) {
        cin >> koczkodany;
        kaczki.push_back(koczkodany);
    }

    if ((h%kaczki[0] != 0 || w%kaczki[0] != 0) || (h<kaczki[0] || w<kaczki[0])) {
        cout << -1;
        return 0;
    }

    
    long long ilosc_h, ilosc_w;

    long long hi = n-1;
    //long long wj = n-1;

    while (hi>=0 && h/kaczki[hi] == 0) {
        hi--;
    }
    /*while (wj>=0 && w/kaczki[wj] == 0) {
        wj--;
    }*/
    while (hi>=0 && w/kaczki[hi] == 0) {
        hi--;
    }

    if (hi<0) {
        hi = 0;
    }
    /*if (wj<0) {
        wj = 0;
    }*/
    ilosc_h = h/kaczki[hi];
    //ilosc_w = w/kaczki[wj];
    ilosc_w = w/kaczki[hi];
    result += ilosc_h*ilosc_w;

    licz_rek(h-kaczki[hi]*ilosc_h, kaczki[hi]*ilosc_w, hi, hi);
    licz_rek(kaczki[hi]*ilosc_h, w-kaczki[hi]*ilosc_w, hi, hi-1);
    licz_rek(h-kaczki[hi]*ilosc_h, w-kaczki[hi]*ilosc_w, hi-1, hi-1);
    cout<<result;
    return 0;
}