#include<bits/stdc++.h>
using namespace std;
int n, h, w;
int k[35];
long long policz_wynik(int height, int width){
if(height < width)
return policz_wynik(width, height);
// printf("policz_wynik(%d, %d)\n", height, width);
if(width == 0){
return 0;
}
int x = 1;
while(k[x] <= width){
x++;
}
x--;
int ile_w_rzedzie = width / k[x];
int ile_rzedow = height / k[x];
int wysokosc = ile_rzedow * k[x];
int szerokosc = ile_w_rzedzie * k[x];
return (long long) ile_w_rzedzie * (long long) ile_rzedow + policz_wynik(height - wysokosc, width) + policz_wynik(wysokosc, width - szerokosc);
}
int main(){
cin >> h >> w;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> k[i];
}
k[n + 1] = 1e9 + 5;
if(h % k[1] != 0 || w % k[1] != 0){
cout << -1;
return 0;
}
cout<< policz_wynik(h, w);
}
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 | #include<bits/stdc++.h> using namespace std; int n, h, w; int k[35]; long long policz_wynik(int height, int width){ if(height < width) return policz_wynik(width, height); // printf("policz_wynik(%d, %d)\n", height, width); if(width == 0){ return 0; } int x = 1; while(k[x] <= width){ x++; } x--; int ile_w_rzedzie = width / k[x]; int ile_rzedow = height / k[x]; int wysokosc = ile_rzedow * k[x]; int szerokosc = ile_w_rzedzie * k[x]; return (long long) ile_w_rzedzie * (long long) ile_rzedow + policz_wynik(height - wysokosc, width) + policz_wynik(wysokosc, width - szerokosc); } int main(){ cin >> h >> w; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> k[i]; } k[n + 1] = 1e9 + 5; if(h % k[1] != 0 || w % k[1] != 0){ cout << -1; return 0; } cout<< policz_wynik(h, w); } |
English