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
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main()
{
    ll h,w,n,zw=0;
    cin>>h>>w>>n;
    vector <ll> oni(n);
    for(int i=0;i<n;i++)
        cin>>oni[n-i-1];
    if(h%oni[n-1]!=0||w%oni[n-1]!=0) {cout<<-1<<"\n"; return 0;}
    int x=0;
    while(h>0&&w>0)
    {
        while(oni[x]>h||oni[x]>w) x++;
        ll y=oni[x],z=x;
        zw+=(h/y)*(w/y);
        ll a=h-h%y,b=w%y;
        while(a>0&&b>0)
        {
            while(oni[z]>a||oni[z]>b) z++;
            zw+=(a/oni[z])*(b/oni[z]);
            b%=oni[z];
        }
        a=h%y,b=w-w%y,z=x;
        while(a>0&&b>0)
        {
            while(oni[z]>a||oni[z]>b) z++;
            zw+=(a/oni[z])*(b/oni[z]);
            a%=oni[z];
        }
        w%=y;
        h%=y;
    }
    cout<<zw<<"\n";
}