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
101
102
103
104
105
106
107
108
109
110
111
112
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>

using namespace std;

struct Pair {
public:
        int x, y;
        Pair(int xx, int yy) { x=min(xx,yy); y=max(xx,yy); }
};

queue<Pair> q;
int n;
int h,w;
vector<int> d;

void print_vector(vector<int> v)
{
    for (auto e : v) {
        printf(" %d", e);
    }
    printf("\n");
}

/* 
   search for element i in vector d such as
       d[i] <= k
   and d[i+1] not exists or d[i+1] > k
 */

int bin_search(int k)
{
        int pos;
        int a = 0;
        int b = n-1;

        while (a <= b) {
                pos = (a+b+1)/2;
                if (d[pos] == k) {
                        return pos;
                }
                if (d[pos] > k) {
                        b = pos-1;
                        continue;
                }
                if (a == b) {
                        return a;
                }
                a=pos;
        }
        assert(1==0);
}

unsigned long long calc_res()
{
        unsigned long long res = 0;
        q.push( Pair(h,w) );
        while(!q.empty()) {
                Pair p = q.front();
                int i, dd, k, l;

                q.pop();
                i = bin_search( p.x );
                dd = d[i];
                k = p.y/dd;
                l = p.x/dd;

//                printf("x=%d, y=%d, d=%d\n", p.x, p.y, dd);

                if (p.x-l*dd > 0) {
//                        printf("put %d %d\n", p.x-dd, dd);
                        q.push( Pair(p.x-l*dd, dd*k) );
                }
                if (p.y-dd*k > 0) {
//                        printf("put %d %d\n", dd, p.y-dd);
                        q.push( Pair( p.x, p.y-dd*k) );
                }
//                if (p.x-dd>0 && p.y-dd>0) {
//                        printf("put %d %d\n", p.x-dd, p.y-dd);
//                        q.push( Pair(p.x-dd, p.y-dd) );
//                }
                res += ((unsigned long long) k) * ((unsigned long long) l);
        }
        return res;
}


int main()
{
        scanf("%d %d\n", &h, &w);
        scanf("%d\n", &n);

        for(int i = 0; i < n; ++i) {
                int dd;
		scanf("%d", &dd);
                d.push_back(dd);
	}

        if (h % d[0] != 0 || w % d[0] != 0) {
                printf("-1\n");
                return 0;
        }

        printf("%llu\n", calc_res());

        return 0;
}