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
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#ifndef DEB_VAL
  #define DEB_VAL 0
#endif
#define DEB if(debug)

#define MP make_pair
#define PB push_back
#define FT first
#define SD second


#define MAX_N 35

int debug = DEB_VAL;

long long tab[MAX_N];
long long h,w;
long long uh,uw;
long long res;
int n;

int main() {
  scanf("%lld %lld", &h, &w);
  scanf("%d", &n);
  for(int i=0;i<n;i++) {
    scanf("%lld", tab+i);
  }
  int k=n-1;
  while(k>=0 && tab[k]>w && tab[k]>h) k--;
  if(k>=0) {
    DEB { printf("Największy: %lld\n", tab[k]); }
    res+=(h/tab[k])*(w/tab[k]);
    uw=(w/tab[k])*tab[k];
    uh=(h/tab[k])*tab[k];
    DEB { printf("Zajął: %lld %lld\n", uw, uh); }
    for(int i=k-1;i>=0;i--) {
      DEB { printf("Sprawdzam %lld\n", tab[i]); }
      res+=((w-uw)/tab[i])*(uh/tab[i]);
      DEB { printf("Res: %lld\n", res); }
      uw+=((w-uw)/tab[i])*tab[i];
      res+=((h-uh)/tab[i])*(uw/tab[i]);
      DEB { printf("Res: %lld\n", res); }
      uh+=((h-uh)/tab[i])*tab[i];
    }
    if(uw==w && uh==h) {
      printf("%lld\n", res);
    } else {
      printf("-1\n");
    }
  } else {
    printf("-1\n");
  }
  return 0;
}