#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define gc getchar_unlocked
using namespace std;
typedef long long ll;
template<typename T> void wczytaj(T &a){
int c = gc();
while(c < '0' || c > '9') c = gc();
for(a = 0; c >= '0' && c <= '9'; c = gc()) a = a*10+c-'0';
}
void solve(){
ll a, b; int n;
wczytaj(a), wczytaj(b), wczytaj(n);
ll wyn = 0ll, pop = 0ll;
while(n--){
int teraz;
wczytaj(teraz);
if(!pop){
if(a%teraz || b%teraz) return void(printf("-1"));
a /= teraz, b /= teraz;
}
else{
ll t = teraz/pop;
wyn += (a%t)*b, a -= a%t;
wyn += (b%t)*a, b -= b%t;
a /= t, b /= t;
}
pop = teraz;
}
wyn += a*b;
printf("%lld", wyn);
}
int main(){
solve();
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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked using namespace std; typedef long long ll; template<typename T> void wczytaj(T &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = a*10+c-'0'; } void solve(){ ll a, b; int n; wczytaj(a), wczytaj(b), wczytaj(n); ll wyn = 0ll, pop = 0ll; while(n--){ int teraz; wczytaj(teraz); if(!pop){ if(a%teraz || b%teraz) return void(printf("-1")); a /= teraz, b /= teraz; } else{ ll t = teraz/pop; wyn += (a%t)*b, a -= a%t; wyn += (b%t)*a, b -= b%t; a /= t, b /= t; } pop = teraz; } wyn += a*b; printf("%lld", wyn); } int main(){ solve(); return 0; } |
English