#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<char> VC;
typedef vector<vector<char>> VVC;
typedef vector<bool> VB;
typedef vector<vector<bool>> VVB;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<pair<int, int>> VPI;
typedef vector<pair<long long, long long>> VPL;
#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
// count, max_width
pair<ll, ll> compute(VL vec, ll h, ll w) {
ll left = h;
ll cur_max_width = 0;
ll ans = 0;
for(int i = vec.size() - 1; i >= 0; i--) {
if(vec[i] > w) continue;
// ile razy wyzej zmiesci sie vec[i]
ll times = (left / vec[i]);
left -= times * vec[i];
if(cur_max_width != 0) {
// mamy times w gore i cc/ve[i] w bok
ans += times * (cur_max_width / vec[i]);
}
else {
ans += times;
cur_max_width = vec[i];
}
if(left == 0) break;
}
//cerr << "H: " << h << "W: " << w << LINE;
//cerr << ans << SPACE << cur_max_width << LINE << LINE;
return {ans, cur_max_width};
}
void solve()
{
ll h, w;
cin >> h >> w;
int n;
cin >> n;
vector<ll> vec(n);
FOR(i, 0, n) {
ll a;
cin >> a;
vec[i] = a;
}
if(h % vec[0] != 0 || w % vec[0] != 0) {
cout << -1 << LINE;
return;
}
ll left = w;
ll ans = 0;
while(left > 0) {
PLL comp = compute(vec, h, left);
ll times = left / comp.second;
ans += times * comp.first;
left -= times * comp.second;
}
cout << ans << LINE;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--)
solve();
}
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> VI; typedef vector<vector<int>> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<char> VC; typedef vector<vector<char>> VVC; typedef vector<bool> VB; typedef vector<vector<bool>> VVB; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; typedef vector<pair<int, int>> VPI; typedef vector<pair<long long, long long>> VPL; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() // count, max_width pair<ll, ll> compute(VL vec, ll h, ll w) { ll left = h; ll cur_max_width = 0; ll ans = 0; for(int i = vec.size() - 1; i >= 0; i--) { if(vec[i] > w) continue; // ile razy wyzej zmiesci sie vec[i] ll times = (left / vec[i]); left -= times * vec[i]; if(cur_max_width != 0) { // mamy times w gore i cc/ve[i] w bok ans += times * (cur_max_width / vec[i]); } else { ans += times; cur_max_width = vec[i]; } if(left == 0) break; } //cerr << "H: " << h << "W: " << w << LINE; //cerr << ans << SPACE << cur_max_width << LINE << LINE; return {ans, cur_max_width}; } void solve() { ll h, w; cin >> h >> w; int n; cin >> n; vector<ll> vec(n); FOR(i, 0, n) { ll a; cin >> a; vec[i] = a; } if(h % vec[0] != 0 || w % vec[0] != 0) { cout << -1 << LINE; return; } ll left = w; ll ans = 0; while(left > 0) { PLL comp = compute(vec, h, left); ll times = left / comp.second; ans += times * comp.first; left -= times * comp.second; } cout << ans << LINE; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); } |
English