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
69
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int a = 0; //wieksza
    int b = 0; //mniejsza
    int r = 0;
    int n1 = 0;
    int n2 = 0;
    int in = 0;
    int wynik = 0;
    scanf("%d", &a);
    scanf("%d", &in);
    if(in > a) {
        b = a;
        a = in;
    }
    else {
        b = in;
    }
    scanf("%d", &n1);
    int rozmiary[n1] = {0};
    for(int i = 0; i < n1; i++) {
        scanf("%d", &in);
        if(in > b) {
            n2 = i;
            break;
        }
        rozmiary[i] = in;
        n2 = i+1;
    }
    if(a%rozmiary[0] != 0 || b%rozmiary[0] != 0) {
        printf("-1");
        return 0;
    }
    if(b == rozmiary[n2-1]) {
        r = a;
        for(int i = 0; i < n2; i++) {
            if(r == 0) {
                break;
            }
            wynik += (r/rozmiary[n2-1-i]) * (b/rozmiary[n2-1-i]);
            r = r%rozmiary[n2-1-i];
        }
        printf("%d", wynik);
        return 0;
    }
    int liczbaObrazow[n2] = {0};
    r = a;
    for(int i = 0; i < n2; i++) {
        if(r == 0) {
            break;
        }
        wynik += r/rozmiary[n2-1-i] * (b/rozmiary[n2-1-i]);
        r = r%rozmiary[n2-1-i];
    }
    r = b;
    for(int i = 0; i < n2; i++) {
        if(r == 0) {
            break;
        }
        wynik += liczbaObrazow[i] + (r/rozmiary[n2-1-i] * (a/rozmiary[n2-1-i])) - (r/rozmiary[n2-1-i]);
        r = r%rozmiary[n2-1-i];
    }
    printf("%d", wynik);
    return 0;
}