#include <iostream> using namespace std; long long w, h, n, d[30], it, wynik; void policz(long long a, long long b){ if(b==0) return; long long x=a/d[it], y=b/d[it]; wynik+=x*y; if(x==0 or y==0){ it--; policz(a, b); return; } long long a1 = a%d[it], b1 = y*d[it]; for(int i=it-1; i>=0 and a1>0; i--){ x=a1/d[i]; wynik+=x*b1/d[i]; a1=a1%d[i]; } it--; policz(a, b%d[it+1]); } int main(){ cin>>w>>h>>n; for(int i=0; i<n; i++) cin>>d[i]; if(w%d[0]>0 or h%d[0]>0){ cout<<"-1"; return 0; } it=n-1; while(w<d[it] or h<d[it]) it--; policz(w, h); cout<<wynik; }
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 | #include <iostream> using namespace std; long long w, h, n, d[30], it, wynik; void policz(long long a, long long b){ if(b==0) return; long long x=a/d[it], y=b/d[it]; wynik+=x*y; if(x==0 or y==0){ it--; policz(a, b); return; } long long a1 = a%d[it], b1 = y*d[it]; for(int i=it-1; i>=0 and a1>0; i--){ x=a1/d[i]; wynik+=x*b1/d[i]; a1=a1%d[i]; } it--; policz(a, b%d[it+1]); } int main(){ cin>>w>>h>>n; for(int i=0; i<n; i++) cin>>d[i]; if(w%d[0]>0 or h%d[0]>0){ cout<<"-1"; return 0; } it=n-1; while(w<d[it] or h<d[it]) it--; policz(w, h); cout<<wynik; } |