#ifdef _MSC_VER
#ifndef __GNUC__
#pragma warning(disable: 4996)
#endif
#define main main0
#endif
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
vector<ll> d;
ll oblicz(ll h, ll w) {
ll wynik = 0, h1, w1;
if(h < d[0] || w < d[0])
return 0;
vector<ll>::reverse_iterator rit = d.rbegin();
if(h > w)
swap(h, w);
while(*rit > h)
++rit;
wynik = (h / *rit) * (w / *rit);
h1 = h % *rit;
w1 = w % *rit;
wynik += oblicz(h, w1);
wynik += oblicz(h1, w - w1);
return wynik;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
ll n, h, w, wynik = 0;
cin >> h >> w >> n;
d.resize(n);
for(vector<ll>::iterator it = d.begin(); it != d.end(); ++it)
cin >> *it;
if(h % d[0] || w % d[0]) {
cout << -1 << endl;
return 0;
}
wynik = oblicz(h, w);
cout << wynik << endl;
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 45 46 47 48 49 50 51 52 | #ifdef _MSC_VER #ifndef __GNUC__ #pragma warning(disable: 4996) #endif #define main main0 #endif #include <algorithm> #include <iostream> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; vector<ll> d; ll oblicz(ll h, ll w) { ll wynik = 0, h1, w1; if(h < d[0] || w < d[0]) return 0; vector<ll>::reverse_iterator rit = d.rbegin(); if(h > w) swap(h, w); while(*rit > h) ++rit; wynik = (h / *rit) * (w / *rit); h1 = h % *rit; w1 = w % *rit; wynik += oblicz(h, w1); wynik += oblicz(h1, w - w1); return wynik; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); ll n, h, w, wynik = 0; cin >> h >> w >> n; d.resize(n); for(vector<ll>::iterator it = d.begin(); it != d.end(); ++it) cin >> *it; if(h % d[0] || w % d[0]) { cout << -1 << endl; return 0; } wynik = oblicz(h, w); cout << wynik << endl; return 0; } |
English