#include<iostream>
using namespace std;
int n;
long d[30];
long wynik;
void liczObr(long h, long w) {
int i;
if(!h || !w)
return;
i = n - 1;
while(long(h / d[i]) == 0)
--i;
while(long(w / d[i]) == 0)
--i;
wynik += long(h / d[i]) * long(w / d[i]);
liczObr(h % d[i], w);
liczObr(w % d[i], h - h % d[i]);
return;
}
int main() {
ios_base::sync_with_stdio(0);
long h, w;
cin >> h >> w;
cin >> n;
for(int i = 0; i < n; ++i)
cin >> d[i];
if((h % d[0] != 0) || (w % d[0] != 0)) {
cout << "-1";
return 0;
}
liczObr(h, w);
cout << wynik;
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 39 40 | #include<iostream> using namespace std; int n; long d[30]; long wynik; void liczObr(long h, long w) { int i; if(!h || !w) return; i = n - 1; while(long(h / d[i]) == 0) --i; while(long(w / d[i]) == 0) --i; wynik += long(h / d[i]) * long(w / d[i]); liczObr(h % d[i], w); liczObr(w % d[i], h - h % d[i]); return; } int main() { ios_base::sync_with_stdio(0); long h, w; cin >> h >> w; cin >> n; for(int i = 0; i < n; ++i) cin >> d[i]; if((h % d[0] != 0) || (w % d[0] != 0)) { cout << "-1"; return 0; } liczObr(h, w); cout << wynik; return 0; } |
English