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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <cassert>
#include <iostream>
#include <istream>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <string>
#include <cstdlib>
#include <memory>
#include <ctime>
#include <bitset>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;


struct Square {
	int size;
};

class Obr2024
{
	int n;
	long width;
	long height;
	std::vector<Square> squares;

	bool notPossible = false;


public:

	long calculateMinimumSquaresRequired(long widthLocal, long heightLocal) {

		if (widthLocal == 0 || heightLocal == 0) return 0;

		long howMany = 0;

		// Calculate the minimum number of squares required
		for (const auto& square : squares) {
			long amount1 = widthLocal / square.size;
			long amount2 = heightLocal / square.size;

			long squaresToAdd = amount1 * amount2;

			if (squaresToAdd > 0) {
				long x = widthLocal - amount1 * square.size;
				long y = heightLocal - amount2 * square.size;

				howMany += squaresToAdd;
				howMany += calculateMinimumSquaresRequired(amount1 * square.size, y);
				if (notPossible) return -1;

				howMany += calculateMinimumSquaresRequired(x, amount2 * square.size);
				if (notPossible) return -1;

				howMany += calculateMinimumSquaresRequired(x, y);
				if (notPossible) return -1;
				break;
			}
			
		}

		if (howMany == 0) notPossible = true;

		return howMany;
	}

	void virtual Solve()
	{
		cin >> width;
		cin >> height;
		cin >> n;


		for (int i = 0; i < n; i++)
		{
			long val;
			cin >> val;
			Square square;
			square.size = val;
			squares.push_back(square);
		}

		std::sort(squares.begin(), squares.end(), [](const Square& a, const Square& b) {
			return a.size > b.size;
		});

		long minSquaresRequired = calculateMinimumSquaresRequired(width, height);
		if (minSquaresRequired > 0) {
			std::cout << minSquaresRequired;
		}
		else {
			std::cout << "-1";
		}


	}
};

int main()
{
	Obr2024* p = new Obr2024();
	p->Solve();
	delete p;
	return 0;
}