#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 30; ll tab[N]; ll tools[N]; void makeTools(int z, int n, int w){ ll ans = 0, left = w; for(int i = z ; i >= 0 ; i--){ ans += (left / tab[i]) * (tab[z] / tab[i]); left %= tab[i]; } tools[z] = ans; } ll solve(int n, int w, int h){ ll left = h, ans = 0; for(int i = 0 ; i < n ; i++) makeTools(i, n, w); for(int i = n - 1 ; i >= 0 ; i--){ ans += tools[i] * (left / tab[i]); left %= tab[i]; } return ans; } int main(){ int n, w, h; scanf("%d %d\n%d", &h, &w, &n); for(int i = 0 ; i < n ; i++){ scanf("%lld", &tab[i]); } if(w % tab[0] != 0 || h % tab[0] != 0) printf("-1\n"); else printf("%lld\n", solve(n, w, h)); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 30; ll tab[N]; ll tools[N]; void makeTools(int z, int n, int w){ ll ans = 0, left = w; for(int i = z ; i >= 0 ; i--){ ans += (left / tab[i]) * (tab[z] / tab[i]); left %= tab[i]; } tools[z] = ans; } ll solve(int n, int w, int h){ ll left = h, ans = 0; for(int i = 0 ; i < n ; i++) makeTools(i, n, w); for(int i = n - 1 ; i >= 0 ; i--){ ans += tools[i] * (left / tab[i]); left %= tab[i]; } return ans; } int main(){ int n, w, h; scanf("%d %d\n%d", &h, &w, &n); for(int i = 0 ; i < n ; i++){ scanf("%lld", &tab[i]); } if(w % tab[0] != 0 || h % tab[0] != 0) printf("-1\n"); else printf("%lld\n", solve(n, w, h)); } |