#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 37; int n, a[mxN]; int policz(int w,int h){ if(w == 0) return 0; int d = 0; rep(i,1,n) if(a[i] <= w) d = a[i]; int res = (w/d) * (h/d); int rw = w%d; int rh = h%d; res += policz(min(rw, h-rh), max(rw, h-rh)); res += policz(min(rh, w), max(rh, w)); return res; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h,w; cin >> h >> w; cin >> n; rep(i,1,n) cin >> a[i]; sort(a+1,a+n+1); if(w%a[1] != 0 || h%a[1] != 0) cout << "-1\n"; else cout << policz(min(w,h), max(w,h)) << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define int long long #define st first #define nd second #define pb push_back #define mp make_pair #define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i) #define all(a) a.begin(),a.end() #define sz(x) (int)(x).size() const int mxN = 37; int n, a[mxN]; int policz(int w,int h){ if(w == 0) return 0; int d = 0; rep(i,1,n) if(a[i] <= w) d = a[i]; int res = (w/d) * (h/d); int rw = w%d; int rh = h%d; res += policz(min(rw, h-rh), max(rw, h-rh)); res += policz(min(rh, w), max(rh, w)); return res; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h,w; cin >> h >> w; cin >> n; rep(i,1,n) cin >> a[i]; sort(a+1,a+n+1); if(w%a[1] != 0 || h%a[1] != 0) cout << "-1\n"; else cout << policz(min(w,h), max(w,h)) << "\n"; } |