#include <bits/stdc++.h> #define int long long #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000000000000007; int mod=1000000007; int mod1=998244353; const int N=37; int d[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int h,w,n; 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; } int ans=0; h/=d[1]; w/=d[1]; for(int i=n;i>1;i--) d[i]/=d[i-1]; for(int i=1;i<n;i++) { int a=h%d[i+1],b=w%d[i+1]; ans+=a*w+b*h-a*b; h-=a; w-=b; h/=d[i+1]; w/=d[i+1]; } ans+=h*w; cout<<ans; 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 41 42 43 44 45 46 47 48 49 50 51 | #include <bits/stdc++.h> #define int long long #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000000000000007; int mod=1000000007; int mod1=998244353; const int N=37; int d[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int h,w,n; 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; } int ans=0; h/=d[1]; w/=d[1]; for(int i=n;i>1;i--) d[i]/=d[i-1]; for(int i=1;i<n;i++) { int a=h%d[i+1],b=w%d[i+1]; ans+=a*w+b*h-a*b; h-=a; w-=b; h/=d[i+1]; w/=d[i+1]; } ans+=h*w; cout<<ans; return 0; } |