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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <map>

using namespace std;
long long match(long long h, long long w, vector<int> & pic)
{
    if(h==0 or w==0) return 0;

     long long a = max(h,w), b = min(h,w);
     long long sum =0;
     int i =0;
     while(i<pic.size() and sum ==0)
        {
            if(a>= pic[i] and b>=pic[i]) sum =  (a/pic[i]) * (b/pic[i]);
            else ++i;
        }
     return sum + match(a%pic[i],b - b%pic[i], pic ) + match(b%pic[i],a - a%pic[i],pic) + match(a%pic[i], b%pic[i], pic);
}
int main()
{
    ios_base::sync_with_stdio(false);
    long long h, w, n;
    cin>>h>>w>>n;
    vector<int> pic(n,0);
    for(int i=0;i<n;++i) cin>>pic[i];
    if((h%pic[0] !=0) or (w%pic[0] != 0)) cout << -1;
    else
    {
    sort(pic.begin(), pic.end(), greater<int>());
        long long sum = match(h,w,pic);
        cout<<sum;
    }
    return 0;
}