#include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> #include <iomanip> #include <tuple> #include <stack> #include <deque> using namespace std; ///// TEMPLATES typedef long long ll; typedef tuple<ll, ll> ti2; typedef tuple<ll, ll, ll> ti3; typedef tuple<ll, ll, ll, ll> ti4; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; typedef vector<ll> vi; typedef vector<ti2> vi2; typedef vector<ti3> vi3; typedef vector<vi> vvi; typedef set<ll> si; typedef set<ti2> si2; typedef set<ti3> si3; typedef multiset<ll> msi; typedef multiset<ti2> msi2; typedef multiset<ti3> msi3; typedef deque<ll> dqi; typedef deque<ti2> dqi2; typedef deque<ti3> dqi3; template<typename T> using PQS = priority_queue<T, vector<T>, greater<T> >; template<typename T> using PQG = priority_queue<T>; ///// OUT OPERATORS ostream& operator<<(ostream& os, const ti2& x) { os << "{ "; auto [a, b] = x; os << a << ", " << b; os << " }"; return os; } ostream& operator<<(ostream& os, const ti3& x) { os << "{ "; auto [a, b, c] = x; os << a << ", " << b << ", " << c; os << " }"; return os; } ostream& operator<<(ostream& os, const ti4& x) { os << "{ "; auto [a, b, c, d] = x; os << a << ", " << b << ", " << c << ", " << d; os << " }"; return os; } ostream& operator<<(ostream& os, const vi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vs& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ///// IN OPERATORS istream& operator>>(istream& is, ti2& x) { ll a, b; is >> a >> b; x = {a, b}; return is; } istream& operator>>(istream& is, ti3& x) { ll a, b, c; is >> a >> b >> c; x = {a, b, c}; return is; } istream& operator>>(istream& is, ti4& x) { ll a, b, c, d; is >> a >> b >> c >> d; x = {a, b, c, d}; return is; } int log_floor(long long x) { return 64 - __builtin_clzll(max(1LL, x)); } // PLUS ONE! int bit_cnt(ll x) { return __builtin_popcountll(x); } void rev(auto& v) { reverse(v.begin(), v.end()); } void sor(auto& v) { sort(v.begin(), v.end()); } // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣆⠀⢀⣀⣀⣤⣤⣤⣦⣦⣤⣤⣄⣀⣀⠀⢠⣾⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⣿⠟⠀⠀⠀⠀⠀⣀⣤⣤⣤⡀⠀⠀⠀⠀⠀⢀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠘⣿⡿⠿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀⠀⠀⠀⣠⣾⣿⣿⣟⣿⡇⠀⠀⠀⠀⠀⢸⣿⣿⣻⣿⣿⣦⠀⠀⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⠀⠀⠀⣿⣿⣿⣿⣿⡟⢠⣶⣾⣿⣿⣷⣤⢽⣿⣿⣿⣿⣿⡇⠀⠀⣀⣤⣿⣷⣴⣶⣦⣀⡀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣠⣇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⠀⠘⠻⣿⣿⣿⡿⠋⠀⢹⣿⣿⣿⣿⡇⠀⣿⣿⣿⡏⢹⣿⠉⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠹⣿⣿⠿⠋⠀⢤⣀⢀⣼⡄⠀⣠⠀⠈⠻⣿⣿⠟⠀⢸⣿⣇⣽⣿⠿⠿⠿⣿⣅⣽⣿⡇⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣟⠁⠀⠀⠀⠈⣿⣿⣿⡇⠀⠀⠀⠀⢀ // ⠛⠛⠛⠛⠛⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛ // ⠀⠀⠀⠀⠀⠀⠘⠛⠻⢿⣿⣿⣿⣿⣿⠟⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠈⠉⠀⠀⠀⠀ [ STASZEK ] // // inject here void ex() { cout << "-1\n"; exit(0); } ll divide(ll h, ll w, const vi& v, int it) { if (h == 0 || w == 0) return 0LL; if (it == v.size()) ex(); ll h2 = h % v[it]; ll h1 = h - h2; ll w2 = w % v[it]; ll w1 = w - w2; ll res = h1 * w1 / (v[it] * v[it]); res += divide(h1, w2, v, it + 1); res += divide(h2, w, v, it + 1); return res; } void solve(){ ll h, w, n; cin >> w >> h >> n; vi v(n); for (int i = 0; i < n; i ++) { cin >> v[i]; } sor(v); rev(v); cout << divide(h, w, v, 0); } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout << setprecision(15) << fixed; int t = 1; // cin >> t; while (t --) solve(); 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | #include <iostream> #include <map> #include <unordered_map> #include <vector> #include <unordered_set> #include <set> #include <queue> #include <utility> #include <algorithm> #include <cassert> #include <iomanip> #include <tuple> #include <stack> #include <deque> using namespace std; ///// TEMPLATES typedef long long ll; typedef tuple<ll, ll> ti2; typedef tuple<ll, ll, ll> ti3; typedef tuple<ll, ll, ll, ll> ti4; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; typedef vector<ll> vi; typedef vector<ti2> vi2; typedef vector<ti3> vi3; typedef vector<vi> vvi; typedef set<ll> si; typedef set<ti2> si2; typedef set<ti3> si3; typedef multiset<ll> msi; typedef multiset<ti2> msi2; typedef multiset<ti3> msi3; typedef deque<ll> dqi; typedef deque<ti2> dqi2; typedef deque<ti3> dqi3; template<typename T> using PQS = priority_queue<T, vector<T>, greater<T> >; template<typename T> using PQG = priority_queue<T>; ///// OUT OPERATORS ostream& operator<<(ostream& os, const ti2& x) { os << "{ "; auto [a, b] = x; os << a << ", " << b; os << " }"; return os; } ostream& operator<<(ostream& os, const ti3& x) { os << "{ "; auto [a, b, c] = x; os << a << ", " << b << ", " << c; os << " }"; return os; } ostream& operator<<(ostream& os, const ti4& x) { os << "{ "; auto [a, b, c, d] = x; os << a << ", " << b << ", " << c << ", " << d; os << " }"; return os; } ostream& operator<<(ostream& os, const vi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vs& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvb& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const vvi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const msi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi2& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ostream& operator<<(ostream& os, const dqi3& x) { os << "{ "; for (auto e : x) os << e << " "; os << "}"; return os; } ///// IN OPERATORS istream& operator>>(istream& is, ti2& x) { ll a, b; is >> a >> b; x = {a, b}; return is; } istream& operator>>(istream& is, ti3& x) { ll a, b, c; is >> a >> b >> c; x = {a, b, c}; return is; } istream& operator>>(istream& is, ti4& x) { ll a, b, c, d; is >> a >> b >> c >> d; x = {a, b, c, d}; return is; } int log_floor(long long x) { return 64 - __builtin_clzll(max(1LL, x)); } // PLUS ONE! int bit_cnt(ll x) { return __builtin_popcountll(x); } void rev(auto& v) { reverse(v.begin(), v.end()); } void sor(auto& v) { sort(v.begin(), v.end()); } // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣠⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣆⠀⢀⣀⣀⣤⣤⣤⣦⣦⣤⣤⣄⣀⣀⠀⢠⣾⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⣿⠟⠀⠀⠀⠀⠀⣀⣤⣤⣤⡀⠀⠀⠀⠀⠀⢀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠘⣿⡿⠿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀⠀⠀⠀⣠⣾⣿⣿⣟⣿⡇⠀⠀⠀⠀⠀⢸⣿⣿⣻⣿⣿⣦⠀⠀⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⠀⠀⠀⣿⣿⣿⣿⣿⡟⢠⣶⣾⣿⣿⣷⣤⢽⣿⣿⣿⣿⣿⡇⠀⠀⣀⣤⣿⣷⣴⣶⣦⣀⡀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣤⣠⣇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⠀⠘⠻⣿⣿⣿⡿⠋⠀⢹⣿⣿⣿⣿⡇⠀⣿⣿⣿⡏⢹⣿⠉⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣶⣄⠀⠀⠹⣿⣿⠿⠋⠀⢤⣀⢀⣼⡄⠀⣠⠀⠈⠻⣿⣿⠟⠀⢸⣿⣇⣽⣿⠿⠿⠿⣿⣅⣽⣿⡇⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣟⠁⠀⠀⠀⠈⣿⣿⣿⡇⠀⠀⠀⠀⢀ // ⠛⠛⠛⠛⠛⠛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛⠛ // ⠀⠀⠀⠀⠀⠀⠘⠛⠻⢿⣿⣿⣿⣿⣿⠟⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠈⠉⠀⠀⠀⠀ [ STASZEK ] // // inject here void ex() { cout << "-1\n"; exit(0); } ll divide(ll h, ll w, const vi& v, int it) { if (h == 0 || w == 0) return 0LL; if (it == v.size()) ex(); ll h2 = h % v[it]; ll h1 = h - h2; ll w2 = w % v[it]; ll w1 = w - w2; ll res = h1 * w1 / (v[it] * v[it]); res += divide(h1, w2, v, it + 1); res += divide(h2, w, v, it + 1); return res; } void solve(){ ll h, w, n; cin >> w >> h >> n; vi v(n); for (int i = 0; i < n; i ++) { cin >> v[i]; } sor(v); rev(v); cout << divide(h, w, v, 0); } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout << setprecision(15) << fixed; int t = 1; // cin >> t; while (t --) solve(); return 0; } |