#include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define f first #define s second #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; ll n,w,h,kwadraty[31]; int main(){ fast cin >> w >> h >> n; for(int i = 1;i <= n;i++) cin >> kwadraty[i]; sort(kwadraty+1,kwadraty+n); if(w % kwadraty[1] != 0 || h % kwadraty[1] != 0){ cout << -1 << "\n"; return 0; } ll res = 0; for(int i = n;i >= 1;i--){ if(w < kwadraty[i] || h < kwadraty[i]) continue; res += (w/kwadraty[i]) * (h/kwadraty[i]); ll temp_h = h % kwadraty[i],temp_w = kwadraty[i] * (w/kwadraty[i]), j = i-1; while(temp_h){ res += (temp_w/kwadraty[j]) * (temp_h/kwadraty[j]); temp_h %= kwadraty[j]; j--; } w %= kwadraty[i]; } cout << res << "\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 | #include <bits/stdc++.h> #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define f first #define s second #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; ll n,w,h,kwadraty[31]; int main(){ fast cin >> w >> h >> n; for(int i = 1;i <= n;i++) cin >> kwadraty[i]; sort(kwadraty+1,kwadraty+n); if(w % kwadraty[1] != 0 || h % kwadraty[1] != 0){ cout << -1 << "\n"; return 0; } ll res = 0; for(int i = n;i >= 1;i--){ if(w < kwadraty[i] || h < kwadraty[i]) continue; res += (w/kwadraty[i]) * (h/kwadraty[i]); ll temp_h = h % kwadraty[i],temp_w = kwadraty[i] * (w/kwadraty[i]), j = i-1; while(temp_h){ res += (temp_w/kwadraty[j]) * (temp_h/kwadraty[j]); temp_h %= kwadraty[j]; j--; } w %= kwadraty[i]; } cout << res << "\n"; } |