#include "bits/stdc++.h" // Ignacy Boehlke
using namespace std; // XIII LO Szczecin
auto operator<<(auto&o,auto p)->decltype(p.first,o){return o<<'{'<<p.first<<", "<<p.second<<'}';}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#ifdef DEBUG
#define PF(x...)fprintf(stderr,x)
#define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define PF(...)(void)0
#define LOG(...)(void)0
#endif
#define FOR(a,b,c)for(int a=(b);a<=(c);++a)
#define REP(a,b)FOR(a,0,(b)-1)
#define ALL(x)(x).begin(), (x).end()
#define fi first
#define se second
using ll=int64_t;
int main() {
cin.tie(0)->sync_with_stdio(0);
int H, W, n;
cin >> H >> W >> n;
vector<int> a(n);
REP(i, n) cin >> a[i];
if (H % a[0] || W % a[0]) {
cout << "-1\n";
return 0;
}
reverse(ALL(a));
ll res = 0;
auto solve = [&](auto &self, int h, int w, int i) {
if (!h || !w) return;
assert(i < n);
int s = a[i];
if (h < s || w < s) {
self(self, h, w, i + 1);
return;
}
res += (ll)(w / s) * (h / s);
if (h % s == 0) swap(h, w);
if (w % s == 0) {
self(self, w, h % s, i + 1);
return;
}
self(self, w % s, h % s, i + 1);
self(self, w % s, h - h % s, i + 1);
self(self, w - w % s, h % s, i + 1);
};
solve(solve, H, W, 0);
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin auto operator<<(auto&o,auto p)->decltype(p.first,o){return o<<'{'<<p.first<<", "<<p.second<<'}';} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto e:x)o<<(", ")+i<<e,i=0;return o<<'}';} #ifdef DEBUG #define PF(x...)fprintf(stderr,x) #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define PF(...)(void)0 #define LOG(...)(void)0 #endif #define FOR(a,b,c)for(int a=(b);a<=(c);++a) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=int64_t; int main() { cin.tie(0)->sync_with_stdio(0); int H, W, n; cin >> H >> W >> n; vector<int> a(n); REP(i, n) cin >> a[i]; if (H % a[0] || W % a[0]) { cout << "-1\n"; return 0; } reverse(ALL(a)); ll res = 0; auto solve = [&](auto &self, int h, int w, int i) { if (!h || !w) return; assert(i < n); int s = a[i]; if (h < s || w < s) { self(self, h, w, i + 1); return; } res += (ll)(w / s) * (h / s); if (h % s == 0) swap(h, w); if (w % s == 0) { self(self, w, h % s, i + 1); return; } self(self, w % s, h % s, i + 1); self(self, w % s, h - h % s, i + 1); self(self, w - w % s, h % s, i + 1); }; solve(solve, H, W, 0); cout << res << '\n'; } |
English