#include <cstdio> #include <vector> #include <algorithm> using namespace std; long long pow(long long x, int y, int d) { x -= x%d; y -= y%d; return x * y; } int main() { int x,y,n, d[31]; scanf("%d %d %d", &x, &y, &n); for (int i = 0; i < n; i++) scanf("%d", &d[i]); if (x % d[0] != 0 || y % d[0] != 0) { puts("-1"); return 0; } long long ret = 0; for (int i = 0; i < n; i++) { ret += (pow(x, y, d[i]) - pow(x, y, (i == n-1 ? x+1 : d[i+1]))) / (((long long)d[i]) * d[i]); } printf("%lld\n", ret); 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; long long pow(long long x, int y, int d) { x -= x%d; y -= y%d; return x * y; } int main() { int x,y,n, d[31]; scanf("%d %d %d", &x, &y, &n); for (int i = 0; i < n; i++) scanf("%d", &d[i]); if (x % d[0] != 0 || y % d[0] != 0) { puts("-1"); return 0; } long long ret = 0; for (int i = 0; i < n; i++) { ret += (pow(x, y, d[i]) - pow(x, y, (i == n-1 ? x+1 : d[i+1]))) / (((long long)d[i]) * d[i]); } printf("%lld\n", ret); return 0; } |