#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll K = 30 + 5; ll k = 0, d[K] = {}; ll ans = 0; inline ll calc(ll n, ll m){ for(ll i = k ; i >= 1 ; i --) if(d[i] <= n && d[i] <= m){ ll x = n / d[i], y = m / d[i]; return x * y + calc(x * d[i], m - y * d[i]) + calc(n - x * d[i], y * d[i]) + calc(n - x * d[i], m - y * d[i]); } return 0; } int main(){ ll n = 0, m = 0; scanf("%lld %lld %lld", &n, &m, &k); for(ll i = 1 ; i <= k ; i ++) scanf("%lld", &d[i]); if(n % d[1] || m % d[1]){ printf("-1"); return 0; } printf("%lld", calc(n, m)); 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll K = 30 + 5; ll k = 0, d[K] = {}; ll ans = 0; inline ll calc(ll n, ll m){ for(ll i = k ; i >= 1 ; i --) if(d[i] <= n && d[i] <= m){ ll x = n / d[i], y = m / d[i]; return x * y + calc(x * d[i], m - y * d[i]) + calc(n - x * d[i], y * d[i]) + calc(n - x * d[i], m - y * d[i]); } return 0; } int main(){ ll n = 0, m = 0; scanf("%lld %lld %lld", &n, &m, &k); for(ll i = 1 ; i <= k ; i ++) scanf("%lld", &d[i]); if(n % d[1] || m % d[1]){ printf("-1"); return 0; } printf("%lld", calc(n, m)); return 0; } |