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
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>

using namespace std;
long long n,h,w, score=0;
long long tab[32];

void calculate_pics(long long w, long long h, long long &counter){
    for(int i = n-1;i>=0;i--){
        if(tab[i] <= w && tab[i] <= h){
            counter += (w/tab[i]) * (h/tab[i]);
            calculate_pics(w - (w/tab[i]) * tab[i], h - (h - (h/tab[i]) * tab[i]), counter);
            calculate_pics(w - (w - (w/tab[i]) * tab[i]), h - (h/tab[i]) * tab[i], counter);
            calculate_pics(w - (w/tab[i]) * tab[i], h - (h/tab[i]) * tab[i], counter);
            break;
        }
    }
}
int main() {
    cin>>w>>h>>n;
    for(int i=0;i<n;i++){
        cin>>tab[i];
    }
    if(w % tab[0] != 0 || h % tab[0] != 0){
        cout<<-1;
        return 0;
    }
    calculate_pics(w, h, score);
    cout<<score;
    return 0;
}