#include <iostream> #include <algorithm> #include <vector> using namespace std; vector<long long int> aa; long long int count(long long int x, long long int y, long long int n) { if(x == 0 || y == 0) { return 0; } while(aa[n] > x || aa[n] > y) { n--; } long long int x_left = x % aa[n]; long long int y_left = y % aa[n]; return ((x - x_left)/aa[n] * (y - y_left)/aa[n]) + count(x - x_left, y_left, n) + count(x_left, y - y_left, n) + count(x_left, y_left, n); } int main() { long long int x, y, n, a; cin >> x >> y >> n; for(int i = 0; i < n; i++) { cin >> a; aa.push_back(a); } if(x % aa[0] != 0 || y % aa[0] != 0) { cout << "-1"; } else { long long int result = count(x, y, n-1); cout << result; } 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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<long long int> aa; long long int count(long long int x, long long int y, long long int n) { if(x == 0 || y == 0) { return 0; } while(aa[n] > x || aa[n] > y) { n--; } long long int x_left = x % aa[n]; long long int y_left = y % aa[n]; return ((x - x_left)/aa[n] * (y - y_left)/aa[n]) + count(x - x_left, y_left, n) + count(x_left, y - y_left, n) + count(x_left, y_left, n); } int main() { long long int x, y, n, a; cin >> x >> y >> n; for(int i = 0; i < n; i++) { cin >> a; aa.push_back(a); } if(x % aa[0] != 0 || y % aa[0] != 0) { cout << "-1"; } else { long long int result = count(x, y, n-1); cout << result; } return 0; } |