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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* i love undocumented code */
/* IT WORKS AHGAGHGAA (at least i think so lmao) */

#include <iostream>
#include <vector>

long long int check_paintings(int x, int y, std::vector<int> paintings)
{
	if (x % paintings[0] != 0 || y % paintings[0] != 0)
	{
		return -1;
	}

	if (x < y)
	{
		std::swap(x, y);
	}

	int temp_y = y;

	long long int temp_res = 0;
	long long int res = 0;
	int prev = 0;
	long long int l;

	for (int j = paintings.size() - 1; j >= 0; j--)
	{
		if (temp_y < paintings[j])
		{
			continue;
		}

		temp_res = 0;
		int temp_x = x;
		for (int i = j; i >= 0; i--)
		{
			if (temp_x < paintings[i])
			{
				continue;
			}

			l = temp_x / paintings[i];
			if (i == j)
			{
				temp_res += l;
			}
			else
			{
				temp_res += l * (prev / paintings[i]) * (paintings[j] / prev);
			}

			temp_x = temp_x % paintings[i];
			prev = paintings[i];
		}
		res += temp_res * (temp_y / paintings[j]);
		temp_y -= paintings[j] * (temp_y / paintings[j]);
	}

	return res;
}

int main()
{
	int x, y;
	std::cin >> x >> y;
	int n;
	std::cin >> n;

	int temp;
	std::vector<int> obr;
	for (int i = 0; i < n; i++)
	{
		std::cin >> temp;
		obr.push_back(temp);
	}

	std::cout << check_paintings(x, y, obr);

	return 0;
}