#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxN = 50+7;
int h,w,n,a[maxN];
int pokryj(int x, int y, int val){
return (x/val) * (y/val);
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>h>>w;
if(h < w)
swap(h,w);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
if(h % a[1] != 0 || w % a[1] != 0){
cout<<-1<<"\n";
return 0;
}
int lh = 0, lw = 0, ans = 0;
a[n+1] = h+1;
for(int i=n;i>=1;i--){
if(a[i] > min(h,w))
continue;
int ch = h / a[i] * a[i];
int cw = w / a[i] * a[i];
int diffh = ch - lh;
int diffw = cw - lw;
//cout<<diffh<<' '<<diffw<<"to jest to\n";
//cout<<ch<<' '<<cw<<"\n";
ans += pokryj(diffh, cw, a[i]);
if(a[i+1] <= min(h,w))
ans += pokryj(diffw, ch, a[i]);
if(a[i+1] <= min(h,w) && diffh && diffw)
ans -= pokryj(diffh, diffw, a[i]);
lh = h / a[i] * a[i];
lw = w / a[i] * a[i];
//cout<<ans<<" to jest ans\n";
}
cout<<ans<<"\n";
}
/*
6 10
3
1 3 6
7 10
3
1 3 6
*/
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 | #include <bits/stdc++.h> using namespace std; #define int long long constexpr int maxN = 50+7; int h,w,n,a[maxN]; int pokryj(int x, int y, int val){ return (x/val) * (y/val); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>h>>w; if(h < w) swap(h,w); cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; if(h % a[1] != 0 || w % a[1] != 0){ cout<<-1<<"\n"; return 0; } int lh = 0, lw = 0, ans = 0; a[n+1] = h+1; for(int i=n;i>=1;i--){ if(a[i] > min(h,w)) continue; int ch = h / a[i] * a[i]; int cw = w / a[i] * a[i]; int diffh = ch - lh; int diffw = cw - lw; //cout<<diffh<<' '<<diffw<<"to jest to\n"; //cout<<ch<<' '<<cw<<"\n"; ans += pokryj(diffh, cw, a[i]); if(a[i+1] <= min(h,w)) ans += pokryj(diffw, ch, a[i]); if(a[i+1] <= min(h,w) && diffh && diffw) ans -= pokryj(diffh, diffw, a[i]); lh = h / a[i] * a[i]; lw = w / a[i] * a[i]; //cout<<ans<<" to jest ans\n"; } cout<<ans<<"\n"; } /* 6 10 3 1 3 6 7 10 3 1 3 6 */ |
English