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
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;

int T[100];

map<pair<int,int>, ll> mapa;

ll f(int x, int y, int k){
    if(x == 0 || y == 0)
        return 0;
    if(x < y)
        swap(x,y);
    
    //x jest wiekszy, szukamy ostatniego ktory miesci sie w y
    int first = 0;
    for(int i = k; i >= 1; i--){
        if(T[i] <= y){
            first = i;
            break;
        }   
    }
    ll x2 = x;
    ll ans = 0;
    ll height = y/T[first] * T[first];
    for(int i = first; i >= 1; i--){
        ll p = height/T[i];
        ll p2 = x/T[i];
        // printf("%lld %lld %lld %lld %lld %lld *\n",p,p2,p * p2,x,y,height);
        ans += p * p2;
        x %= T[i];
    }
    return ans + f(x2, y-height, k);
}

int main(void){
    int h,w;
    scanf("%d %d",&h,&w);
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &T[i]);
    }
    if(h % T[1] != 0 || w % T[1] != 0){
        printf("-1\n");
        return 0;
    }
    ll ans = f(h,w,n);
    printf("%lld\n",ans);

    return 0;
}