x,y=[int(i) for i in input().split()] n=int(input()) t=[int(i) for i in input().split()] def oblicz(x,y,k): if x>y:x,y=y,x while t[k]>x and k>0:k-=1 xk=x//t[k] yk=y//t[k] if x==0 or y==0: return 0 else: return (xk*yk)+min(oblicz(x-xk*t[k],y,k)+oblicz(xk*t[k],y-yk*t[k],k),oblicz(x,y-yk*t[k],k)+oblicz(x-xk*t[k],yk*t[k],k)) if x%t[0] or y%t[0]: print(-1) else: print(oblicz(x,y,n-1))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | x,y=[int(i) for i in input().split()] n=int(input()) t=[int(i) for i in input().split()] def oblicz(x,y,k): if x>y:x,y=y,x while t[k]>x and k>0:k-=1 xk=x//t[k] yk=y//t[k] if x==0 or y==0: return 0 else: return (xk*yk)+min(oblicz(x-xk*t[k],y,k)+oblicz(xk*t[k],y-yk*t[k],k),oblicz(x,y-yk*t[k],k)+oblicz(x-xk*t[k],yk*t[k],k)) if x%t[0] or y%t[0]: print(-1) else: print(oblicz(x,y,n-1)) |