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
60
61
62
63
64
65
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int w,h;

std::vector<int> sizes;

long long fill1(int x, int y, int last_val_index) // x>=y
{
    if( x < y )
        swap(x,y);
        
    long long ans = 0;
    for( int i = last_val_index ; i >=0 ; i --)
    {
        if( y >= sizes[i] )
        {
            long long tmp = y / sizes[i];
            long long tmp2 = x / sizes[i];
            ans += tmp * tmp2;
            ans += fill1( x - tmp2 * sizes[i], y, i);
            ans += fill1( tmp2* sizes[i], y - tmp * sizes[i], i);
            return ans;
        }
    }
    return 0;
}

int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>h>>w;
    
    cin>>n;
    
    for(int i = 0 ; i < n ; i ++)
    {
        int a;
        cin>>a;
        sizes.push_back(a);
    }
    
    sort(sizes.begin(), sizes.end());
    
    if( w % sizes[0] != 0 || h % sizes[0] != 0)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    
    cout<<fill1(w,h,sizes.size()-1)<<endl;
        
}