#include <ios>
#include <functional>
#include <cassert>
#include <vector>
#define REP(i, n) for(int i=0; i<(n); ++i)
#define SREP(n) REP(_ ## n, n)
#define FOR(i, p, n) for(int i=(p); i<=(n); ++i)
#define RFOR(i, p, n) for(int i=(p); i>=(n); --i)
#define pb push_back
#define eb emplace_back
#define C const
#define V std::vector
#define F std::function
#define R std::ranges
#define RV std::ranges::views
#define sz(A) int(A.size())
#define all(A) A.begin(), A.end()
#define rall(A) A.rbegin(), A.rend()
typedef long long ll;
typedef long double ld;
typedef V <int> vi;
typedef V <vi> vvi;
typedef V <bool> vb;
typedef V <char> vc;
typedef V <ll> vll;
typedef const int ci;
typedef const ll cll;
int I(){
int z;
while ((z=getchar_unlocked())<'0'||z>'9');
int r=z-'0';
while ((z=getchar_unlocked())>='0'&&z<='9')
r=r*10+z-'0';
return r;
}
int main(){
int x=I(),y=I();
ci n=I();
vi dziel(n);
REP(i, n)
dziel[i]=I();
if (x%dziel[0]||y%dziel[0]){
printf("-1\n");
exit(0);
}
ll w=0;
while (x&&y){
ci l=dziel.back();
if (l>x||l>y){
dziel.pop_back();
continue;
}
ci nx=x%l,ny=y%l;
w+=ll(x/l)*(y/l);
auto pros=[&](int a, int b) -> void{
assert(!(a%l));
RFOR(i, sz(dziel)-1, 0){
ci j=dziel[i];
w+=ll(a/j)*(b/j);
b%=j;
}
assert(!b);
};
pros(x-nx, ny),pros(y-ny, nx);
x=nx,y=ny;
}
printf("%lld\n", 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <ios> #include <functional> #include <cassert> #include <vector> #define REP(i, n) for(int i=0; i<(n); ++i) #define SREP(n) REP(_ ## n, n) #define FOR(i, p, n) for(int i=(p); i<=(n); ++i) #define RFOR(i, p, n) for(int i=(p); i>=(n); --i) #define pb push_back #define eb emplace_back #define C const #define V std::vector #define F std::function #define R std::ranges #define RV std::ranges::views #define sz(A) int(A.size()) #define all(A) A.begin(), A.end() #define rall(A) A.rbegin(), A.rend() typedef long long ll; typedef long double ld; typedef V <int> vi; typedef V <vi> vvi; typedef V <bool> vb; typedef V <char> vc; typedef V <ll> vll; typedef const int ci; typedef const ll cll; int I(){ int z; while ((z=getchar_unlocked())<'0'||z>'9'); int r=z-'0'; while ((z=getchar_unlocked())>='0'&&z<='9') r=r*10+z-'0'; return r; } int main(){ int x=I(),y=I(); ci n=I(); vi dziel(n); REP(i, n) dziel[i]=I(); if (x%dziel[0]||y%dziel[0]){ printf("-1\n"); exit(0); } ll w=0; while (x&&y){ ci l=dziel.back(); if (l>x||l>y){ dziel.pop_back(); continue; } ci nx=x%l,ny=y%l; w+=ll(x/l)*(y/l); auto pros=[&](int a, int b) -> void{ assert(!(a%l)); RFOR(i, sz(dziel)-1, 0){ ci j=dziel[i]; w+=ll(a/j)*(b/j); b%=j; } assert(!b); }; pros(x-nx, ny),pros(y-ny, nx); x=nx,y=ny; } printf("%lld\n", w); } |
English