#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back ll t[39]; int leg = 1; ll w = 0; ll a, b, n; bool fail = false; void rob(){ if(a == 0 || b == 0) return; if(leg > n){ cout<<-1<<endl; fail = true; return; } while(t[leg] > a || t[leg] > b){ leg++; if(leg > n){ cout<<-1<<endl; fail = true; return; } } if(leg > n){ cout<<-1<<endl; fail = true; return; } ll il = a / t[leg], ilb = b / t[leg]; ll wpr = il * t[leg], wd = ilb * t[leg]; w += il * ilb; ll da = wpr, db = b - wd, pa = a - wpr, pb = wd; for(int i = leg + 1; i <= n; i++){ // dolny if(db > 0){ ll uzywd = db / t[i]; db -= uzywd * t[i]; w += uzywd * (da / t[i]); } // prawy if(pa > 0){ ll uzywp = pa / t[i]; pa -= uzywp * t[i]; w += uzywp * (pb / t[i]); } } if(db > 0 || pa > 0){ cout<<-1<<endl; fail = true; return; } a = a - wpr; b = b - wd; leg++; rob(); } int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int ww, h; cin>>ww>>h>>n; for(int i = 1; i <= n; i++) cin>>t[n - i + 1]; a = ww; b = h; rob(); if(!fail) cout<<w<<endl; } /* */
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back ll t[39]; int leg = 1; ll w = 0; ll a, b, n; bool fail = false; void rob(){ if(a == 0 || b == 0) return; if(leg > n){ cout<<-1<<endl; fail = true; return; } while(t[leg] > a || t[leg] > b){ leg++; if(leg > n){ cout<<-1<<endl; fail = true; return; } } if(leg > n){ cout<<-1<<endl; fail = true; return; } ll il = a / t[leg], ilb = b / t[leg]; ll wpr = il * t[leg], wd = ilb * t[leg]; w += il * ilb; ll da = wpr, db = b - wd, pa = a - wpr, pb = wd; for(int i = leg + 1; i <= n; i++){ // dolny if(db > 0){ ll uzywd = db / t[i]; db -= uzywd * t[i]; w += uzywd * (da / t[i]); } // prawy if(pa > 0){ ll uzywp = pa / t[i]; pa -= uzywp * t[i]; w += uzywp * (pb / t[i]); } } if(db > 0 || pa > 0){ cout<<-1<<endl; fail = true; return; } a = a - wpr; b = b - wd; leg++; rob(); } int main(){ ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int ww, h; cin>>ww>>h>>n; for(int i = 1; i <= n; i++) cin>>t[n - i + 1]; a = ww; b = h; rob(); if(!fail) cout<<w<<endl; } /* */ |