#include<bits/stdc++.h> using namespace std; const long long maxi = 1e+9+10; vector <long long> v; unordered_map <long long,long long> M; long long rek(int a , int b , int akt) { long long licz = 0; long long am , bm; if(a > b)swap(a,b); if(M[a*maxi+b]) return M[a*maxi+b]; if(akt == -1)return 0; if(a < v[akt])return rek(a,b,akt-1); licz += a/(v[akt])*(b/(v[akt])); am = a%v[akt]; bm = b%v[akt]; if(am > bm) { licz += rek(am,b,akt-1); licz += rek(bm,a-am,akt-1); } else { licz += rek(bm,a,akt-1); licz += rek(am,b-bm,akt-1); } M[a*maxi+b] = licz; return licz; } int main() { int n , a , h , w; ios_base::sync_with_stdio(0); cin.tie(0); cin >> h >> w; cin >> n; for(int i =0 ; i < n; i++) { cin >> a; v.push_back(a); } if(h%v[0] || w%v[0]) cout << -1; else cout << rek(h,w,n-1); }
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 52 53 | #include<bits/stdc++.h> using namespace std; const long long maxi = 1e+9+10; vector <long long> v; unordered_map <long long,long long> M; long long rek(int a , int b , int akt) { long long licz = 0; long long am , bm; if(a > b)swap(a,b); if(M[a*maxi+b]) return M[a*maxi+b]; if(akt == -1)return 0; if(a < v[akt])return rek(a,b,akt-1); licz += a/(v[akt])*(b/(v[akt])); am = a%v[akt]; bm = b%v[akt]; if(am > bm) { licz += rek(am,b,akt-1); licz += rek(bm,a-am,akt-1); } else { licz += rek(bm,a,akt-1); licz += rek(am,b-bm,akt-1); } M[a*maxi+b] = licz; return licz; } int main() { int n , a , h , w; ios_base::sync_with_stdio(0); cin.tie(0); cin >> h >> w; cin >> n; for(int i =0 ; i < n; i++) { cin >> a; v.push_back(a); } if(h%v[0] || w%v[0]) cout << -1; else cout << rek(h,w,n-1); } |