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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=35;
int n;
ll w,h;
ll cost[MAXN];
ll d[MAXN];
int main()
{
    cin>>h>>w>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>d[i];
    }
    if(w%d[1]!=0 or h%d[1]!=0)
    {
        cout<<-1;
        return 0;
    }
   // cost[1]=w/d[1];
    for(int i=1; i<=n; i++)
    {
        ll cur=h;
        for(int j=i; j>=1; j--)
        {
            cost[i]+=(cur/d[j])*(d[i]/d[j]);
            cur-=(cur/d[j])*d[j];
        }
    }
    ll res=0;
    for(int i=n; i>=1; i--)
    {
        res+=cost[i]*(w/d[i]);
        w-=(w/d[i])*d[i];
    }
    cout<<res;
    return 0;
}