#include "bits/stdc++.h" #define int long long using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int w, h; cin >> w >> h; int n; cin >> n; deque<int> A(n); for(auto &a : A) cin >> a; sort(A.begin(), A.end()); if(w % A[0] != 0 || h % A[0] != 0) { cout <<"-1\n"; return 0; } int tmp = A[0]; w/=tmp; h/=tmp; for(int i=A.size()-1; i>=0; i--) A[i]/=tmp; int res=0; while(true){ if(w == 1 && h == 1) { res++; break; } if(A.size() == 1) { if(h % A[0] == 0 && w % A[0] == 0) { res+=h*w/(A[0]*A[0]); break; } else { cout << "-1\n"; return 0; } } int next = A[1]; if((w % next == 0) && (h % next == 0)){ w /= next; h /= next; A.pop_front(); for(auto &a : A) a /= next; continue; } if(w % next == 0) { int ile_trzeba = h % next; h-=ile_trzeba; res += ile_trzeba * w; w/=next; h/=next; for(int i=A.size()-1; i>=0; i--) A[i]/=next; continue; } if(h % next == 0) { int ile_trzeba = w % next; w-=ile_trzeba; res += ile_trzeba * h; w/=next; h/=next; for(int i=A.size()-1; i>=0; i--) A[i]/=next; continue; } int ile_trzeba = w % next; w-=ile_trzeba; res += ile_trzeba * h; continue; } cout << res << '\n'; return 0; }
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include "bits/stdc++.h" #define int long long using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int w, h; cin >> w >> h; int n; cin >> n; deque<int> A(n); for(auto &a : A) cin >> a; sort(A.begin(), A.end()); if(w % A[0] != 0 || h % A[0] != 0) { cout <<"-1\n"; return 0; } int tmp = A[0]; w/=tmp; h/=tmp; for(int i=A.size()-1; i>=0; i--) A[i]/=tmp; int res=0; while(true){ if(w == 1 && h == 1) { res++; break; } if(A.size() == 1) { if(h % A[0] == 0 && w % A[0] == 0) { res+=h*w/(A[0]*A[0]); break; } else { cout << "-1\n"; return 0; } } int next = A[1]; if((w % next == 0) && (h % next == 0)){ w /= next; h /= next; A.pop_front(); for(auto &a : A) a /= next; continue; } if(w % next == 0) { int ile_trzeba = h % next; h-=ile_trzeba; res += ile_trzeba * w; w/=next; h/=next; for(int i=A.size()-1; i>=0; i--) A[i]/=next; continue; } if(h % next == 0) { int ile_trzeba = w % next; w-=ile_trzeba; res += ile_trzeba * h; w/=next; h/=next; for(int i=A.size()-1; i>=0; i--) A[i]/=next; continue; } int ile_trzeba = w % next; w-=ile_trzeba; res += ile_trzeba * h; continue; } cout << res << '\n'; return 0; } |