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
#include <bits/stdc++.h>
#define ll long long
#define llu long long unsigned
#define ld long double
#define fr(i,n) for(int i=0;i<n;i++)
#define watch(x) cout<<(#x)<<"=="<<(x)<<endl
#define ft first
#define sc second
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define P 1000000007llu
#define N 35
#define M 1000001
#define LC 262144
using namespace std;

int getchar_pos_int() {
    int n=0, c=getchar();
    while('0'<=c&&c<='9') {n=n*10+c-'0'; c=getchar();}
    return n;
}

int d[N];


ll f(int h, int w, int i) {
    //printf("Preorder: h=%d w=%d i=%d\n",h,w,i);
    if(!h||!w) return 0;
    if(i<0&&h>0&&w>0) return -1;///!!!
    if(d[i]>min(h,w)) return f(h,w,i-1);
    int x=(w/d[i])*d[i], y=(h/d[i])*d[i];
    //printf("preorder h=%d y=%d w=%d x=%d\n",h,y,w,x);
    ll f1=f(h-y,x,i-1), f2=f(y,w-x,i-1), f3=f(h-y,w-x,i-1);
    //watch(w);watch(h);watch(i);watch(d[i]);watch(x);watch(y);watch(f1);watch(f2);watch(f3);
    if(f1==-1||f2==-1||f3==-1) return -1;
    //printf("Postorder: x/d[%d]=%d y/d[%d]=%d res=%lld\n",i,x/d[i],i,y/d[i],(x/d[i])*(y/d[i])+f1+f2+f3);
    return (x/d[i])*(y/d[i])+f1+f2+f3;
}

int main() {
    int h=getchar_pos_int(),w=getchar_pos_int(),n=getchar_pos_int();
    fr(i,n) d[i]=getchar_pos_int();
    printf("%lld",f(h,w,n-1));
}