#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef long long int ll;
vector<int> arr;
int main(){
int h, w;
int n;
cin >> h >> w;
if(h > w)
swap(h, w);
cin >> n;
for(int i = 0 ; i < n; i++){
int a;
cin >> a;
arr.push_back(a);
}
sort(arr.begin(), arr.end(), greater<int>());
int small = arr[arr.size()-1];
if(h%small != 0 or w%small != 0){
cout << -1;
return 0;
}
ll res = 0;
for(int i = 0 ; i < n and h > 0; i++){
int size = arr[i];
int mul = h/size;
h -= (mul*size);
ll add = 0;
int w2 = w;
for(int k = i ; k < n and w2 > 0; k++){
int size2 = arr[k];
int m = w2/size2;
w2 -= (m*size2);
add += (m*(size/size2));
}
res += add*mul;
}
cout << res;
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 | #include<iostream> #include<vector> #include<algorithm> #include<set> using namespace std; typedef long long int ll; vector<int> arr; int main(){ int h, w; int n; cin >> h >> w; if(h > w) swap(h, w); cin >> n; for(int i = 0 ; i < n; i++){ int a; cin >> a; arr.push_back(a); } sort(arr.begin(), arr.end(), greater<int>()); int small = arr[arr.size()-1]; if(h%small != 0 or w%small != 0){ cout << -1; return 0; } ll res = 0; for(int i = 0 ; i < n and h > 0; i++){ int size = arr[i]; int mul = h/size; h -= (mul*size); ll add = 0; int w2 = w; for(int k = i ; k < n and w2 > 0; k++){ int size2 = arr[k]; int m = w2/size2; w2 -= (m*size2); add += (m*(size/size2)); } res += add*mul; } cout << res; return 0; } |
English