#include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int h, w, n; cin >> h >> w >> n; vector<int> pictures(n); rep(i, n) cin >> pictures[i]; ll res = 0; queue<pair<pair<int, int>, int>> q; q.push({{h, w}, n-1}); while (!q.empty()) { auto [hw, i] = q.front(); q.pop(); if (i < 0) { cout << "-1\n"; return 0; } h = hw.st; w = hw.nd; ll ct = (ll) (h / pictures[i]) * (w / pictures[i]); res += ct; int new_h = h % pictures[i]; int new_w = w % pictures[i]; if (new_h > 0) q.push({{new_h, w - new_w}, i-1}); if (new_w > 0) q.push({{h, new_w}, i-1}); } 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 | #include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int h, w, n; cin >> h >> w >> n; vector<int> pictures(n); rep(i, n) cin >> pictures[i]; ll res = 0; queue<pair<pair<int, int>, int>> q; q.push({{h, w}, n-1}); while (!q.empty()) { auto [hw, i] = q.front(); q.pop(); if (i < 0) { cout << "-1\n"; return 0; } h = hw.st; w = hw.nd; ll ct = (ll) (h / pictures[i]) * (w / pictures[i]); res += ct; int new_h = h % pictures[i]; int new_w = w % pictures[i]; if (new_h > 0) q.push({{new_h, w - new_w}, i-1}); if (new_w > 0) q.push({{h, new_w}, i-1}); } cout << res << "\n"; return 0; } |