#include <bits/stdc++.h>
#define LL long long
#define endl '\n'
using namespace std;
vector<LL>tab;
LL w=0,k;
void ez(LL n,LL m,LL nr)
{
if(n<=0||m<=0) return;
if(nr>=k)
{
cout<<-1;
exit(0);
}
if(min(n,m)<tab[nr]) ez(n,m,nr+1);
else
{
w+=(m/tab[nr])*(n/tab[nr]);
LL mm=m%tab[nr];
LL nn=n%tab[nr];
if(mm>nn)
{
ez(n,mm,nr+1);
ez(nn,m-mm,nr+1);
}
else
{
ez(n-nn,mm,nr+1);
ez(nn,m,nr+1);
}
}
}
int main()
{
std::ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
LL n,m;
cin>>n>>m>>k;
tab.resize(k);
for(LL i=0;i<k;i++)cin>>tab[i];
sort(tab.begin(),tab.end(),greater<LL>());
ez(n,m,0);
cout<<w;
}
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 | #include <bits/stdc++.h> #define LL long long #define endl '\n' using namespace std; vector<LL>tab; LL w=0,k; void ez(LL n,LL m,LL nr) { if(n<=0||m<=0) return; if(nr>=k) { cout<<-1; exit(0); } if(min(n,m)<tab[nr]) ez(n,m,nr+1); else { w+=(m/tab[nr])*(n/tab[nr]); LL mm=m%tab[nr]; LL nn=n%tab[nr]; if(mm>nn) { ez(n,mm,nr+1); ez(nn,m-mm,nr+1); } else { ez(n-nn,mm,nr+1); ez(nn,m,nr+1); } } } int main() { std::ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); LL n,m; cin>>n>>m>>k; tab.resize(k); for(LL i=0;i<k;i++)cin>>tab[i]; sort(tab.begin(),tab.end(),greater<LL>()); ez(n,m,0); cout<<w; } |
English