#include <bits/stdc++.h> using namespace std; long long h,w; long long d[40]; int n; long long result; int main(){ cin>>h>>w; cin>>n; for(int i=1;i<=n;i++)cin>>d[i]; if(h%d[1] != 0 || w%d[1] != 0){ cout<<"-1"; return 0; } for(int i=1;i<=n;i++){ result += (h/d[i]) * (w/d[i]); if(i == 1) continue; long long a = (h/d[i]) * d[i]; long long b = (w/d[i]) * d[i]; result -= (a/d[i-1]) * (b/d[i-1]); } cout<<result; }
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 | #include <bits/stdc++.h> using namespace std; long long h,w; long long d[40]; int n; long long result; int main(){ cin>>h>>w; cin>>n; for(int i=1;i<=n;i++)cin>>d[i]; if(h%d[1] != 0 || w%d[1] != 0){ cout<<"-1"; return 0; } for(int i=1;i<=n;i++){ result += (h/d[i]) * (w/d[i]); if(i == 1) continue; long long a = (h/d[i]) * d[i]; long long b = (w/d[i]) * d[i]; result -= (a/d[i-1]) * (b/d[i-1]); } cout<<result; } |