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
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define PB push_back
typedef long long ll;
typedef long double ld;
#define REP(x,y) for(int x=0;x<(y);x++)
#define ROF(x,y) for(int x=(y);x>0;x--)
#define FOR(x,z,y) for(int x=(z);x<=(y);x++)
#define INT(x) int x;scanf("%d",&x)
#define LL(x) ll x;scanf("%lld",&x)
#define CZ(x) char x;scanf(" %c",&x)
typedef pair<int,int> pii;
typedef pair<int,pii> piii;

vector<int> vec;

ll solve(ll h, ll w, int ptr){
    if(h==0 || w == 0) return 0;
    if(ptr<0){
        puts("-1");
        exit(0);
    }
    ll h_n = h/vec[ptr];
    ll w_n = w/vec[ptr];
    return h_n*w_n + solve(h-h_n*vec[ptr],w_n*vec[ptr],ptr-1) + solve(h,w-w_n*vec[ptr],ptr-1);
}


int main(){
    INT(h);INT(w);INT(n);
    REP(i,n){
        INT(a);
        vec.PB(a);
    }
    printf("%lld\n",solve(h,w,vec.size()-1));
}