#include<bits/stdc++.h>
using namespace std;
long long tab[50], wyn, czy;
void rek(long long a, long long b, long long x)
{
if(x == 0)
{
czy = 1;
return;
}
long long c = a / tab[x], d = b / tab[x];
if(c == 0 || d == 0)
{
rek(a, b, x-1);
return;
}
wyn += c * d;
c = a - tab[x] * c;
d = b - tab[x] * d;
if(c > 0) rek(c, b, x-1);
if(d > 0) rek(a-c, d, x-1);
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
long long h, w, n;
cin>>h>>w>>n;
for(int i=1; i<=n; i++) cin>>tab[i];
rek(h, w, n);
if(czy) cout<<-1;
else cout<<wyn;
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 | #include<bits/stdc++.h> using namespace std; long long tab[50], wyn, czy; void rek(long long a, long long b, long long x) { if(x == 0) { czy = 1; return; } long long c = a / tab[x], d = b / tab[x]; if(c == 0 || d == 0) { rek(a, b, x-1); return; } wyn += c * d; c = a - tab[x] * c; d = b - tab[x] * d; if(c > 0) rek(c, b, x-1); if(d > 0) rek(a-c, d, x-1); return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long h, w, n; cin>>h>>w>>n; for(int i=1; i<=n; i++) cin>>tab[i]; rek(h, w, n); if(czy) cout<<-1; else cout<<wyn; return 0; } |
English