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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 1000'007;
int tab[N], dp[N], dp2[N];

inline bool Check(int n, int a, int b, int c)
{
    int m = 0, m2 = 0;
    /*cout << "C: ";
    for(int i = 1; i <= n; ++i)
        cout << tab[i] << " ";
    cout << "\n";*/
    for(int i = 1; i <= n; ++i)
    {
        dp[i] = 0;
        for(int j = 1; j < i; ++j)
            if(tab[j] < tab[i])
                dp[i] = max(dp[i], dp[j]);
        ++dp[i];
        m = max(m, dp[i]);
        if(m > a) return 0;
    }
    //cout << "F: " << m << "\n";
    if(m != a) return 0;
    m = 0;
    for(int i = n; i >= 1; --i)
    {
        dp2[i] = 0;
        for(int j = n; j > i; --j)
            if(tab[j] < tab[i])
                dp2[i] = max(dp2[i], dp2[j]);
        ++dp2[i];
        m = max(m, dp2[i]);
        if(m > b) return 0;
        //cout << i << " " << dp[i] << " " << dp2[i] << "\n";
        m2 = max(m2, dp[i] + dp2[i] - 1);
        if(m2 > c) return 0;
    }
    //cout << "S: " << m << " " << m2 << "\n";
    if(m != b || m2 != c) return 0;
    return 1;
}

void Solve()
{
    int a, b, c, p;
    cin >> a >> b >> c >> p;
    int n = a + b - 1, dod = 1, ans = 0;
    while(dod > 0)
    {
        dod = 0;
        ++n;
        for(int i = 1; i <= n; ++i)
            tab[i] = i;
        do
        {
            dod += (int)Check(n, a, b, c);
        }while(next_permutation(tab + 1, tab + 1 + n));
        if(dod > 0)
            ans = dod;
    }
    cout << n - 1 << " " << ans % p << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}