#include <iostream> using namespace std; typedef long long int lint; int n; const int MAX_DS = 30; lint d[MAX_DS]; lint solve(lint h, lint w, int max_index) { // for (int i = 0; i < n - max_index - 1; ++i) { // cout << " "; // } // cout << "solve(h=" << h << ", w=" << w << ", i=" << max_index << ") = "; lint curr_d = d[max_index]; if (h == 0 || w == 0) { //cout << "0 [end - no space]" << endl; return 0; } lint kh = h / curr_d; lint kw = w / curr_d; if (kh == 0 || kw == 0) { //cout << "deeper" << endl; return solve(h, w, max_index-1); } if (max_index == 0) { //cout << kh * kw << " [end - smallest image]" << endl; return kh * kw; } //cout << kh * kw << " + deeper" << endl; return kh * kw + solve(h - kh * curr_d, kw * curr_d, max_index - 1) + solve(h, w - kw*curr_d, max_index - 1); } int main() { ios_base::sync_with_stdio(0); int h, w; cin >> h >> w; cin >> n; for (int i = 0; i < n; ++i) cin >> d[i]; if (h % d[0] != 0 || w % d[0] != 0) { cout << "-1" << endl; return 0; } cout << solve(h, w, n-1) << endl; 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 | #include <iostream> using namespace std; typedef long long int lint; int n; const int MAX_DS = 30; lint d[MAX_DS]; lint solve(lint h, lint w, int max_index) { // for (int i = 0; i < n - max_index - 1; ++i) { // cout << " "; // } // cout << "solve(h=" << h << ", w=" << w << ", i=" << max_index << ") = "; lint curr_d = d[max_index]; if (h == 0 || w == 0) { //cout << "0 [end - no space]" << endl; return 0; } lint kh = h / curr_d; lint kw = w / curr_d; if (kh == 0 || kw == 0) { //cout << "deeper" << endl; return solve(h, w, max_index-1); } if (max_index == 0) { //cout << kh * kw << " [end - smallest image]" << endl; return kh * kw; } //cout << kh * kw << " + deeper" << endl; return kh * kw + solve(h - kh * curr_d, kw * curr_d, max_index - 1) + solve(h, w - kw*curr_d, max_index - 1); } int main() { ios_base::sync_with_stdio(0); int h, w; cin >> h >> w; cin >> n; for (int i = 0; i < n; ++i) cin >> d[i]; if (h % d[0] != 0 || w % d[0] != 0) { cout << "-1" << endl; return 0; } cout << solve(h, w, n-1) << endl; return 0; } |