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
#include <bits/stdc++.h>
using namespace std;
long long T[31];
int n;
long long ask(long long a,long long b){
    if(a > b)swap(a,b);
    if(a == 0)return 0;
    int maks_fit = 0;
    for (int i = 0; i < n;i++){
        if(T[i] <= a)maks_fit = i;
    }
    long long koszt = (a/T[maks_fit]) * (b/T[maks_fit]);
    return koszt + ask(a % T[maks_fit],b) + ask((a/T[maks_fit])*T[maks_fit],b % T[maks_fit]);

}
int main(){
    int h,w;
    cin >> h >> w;
    cin >> n;
    for (int i = 0; i < n;i++){
        cin >> T[i];
    }
    if(h % T[0] == 0 && w % T[0] == 0){
        cout << ask(h,w);
    }
    else{
        cout << "-1";
    }
}