#include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() ll T[30], n; ll solve(ll h, ll w, ll l) { if(l==-1) { if(h && w) { cout << -1 << '\n'; exit(0); } return 0; } if(!h || !w) return 0; ll ans=(h/T[l])*(w/T[l]); ll x=h%T[l], y=w%T[l]; ans+=solve(x, w-y, l-1)+solve(h-x, y, l-1)+solve(x, y, l-1); return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w >> n; rep(i, n) cin >> T[i]; cout << solve(h, w, n-1) << '\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 | #include<bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() ll T[30], n; ll solve(ll h, ll w, ll l) { if(l==-1) { if(h && w) { cout << -1 << '\n'; exit(0); } return 0; } if(!h || !w) return 0; ll ans=(h/T[l])*(w/T[l]); ll x=h%T[l], y=w%T[l]; ans+=solve(x, w-y, l-1)+solve(h-x, y, l-1)+solve(x, y, l-1); return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w >> n; rep(i, n) cin >> T[i]; cout << solve(h, w, n-1) << '\n'; } |