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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <bits/stdc++.h>
#include "message.h"
#include "futbol.h"

using namespace std;

typedef long long int ll;

const int MAIN_NODE_ID = 0;

ll modularPow(ll base, ll exponent, ll modulus)
{
    if (modulus == 1)
        return 0;
    
    ll result = 1;
    base = base % modulus;
    
    while (exponent > 0)
    {
        if (exponent & 1 == 1)
            result = (result * base) % modulus;
        
        exponent >>= 1;
        base = (base * base) % modulus;
    }
    
    return result;
}

struct Range
{
    int start, end;
};

class Node
{
    public:
        bool needed;
    
        Node(int nodeId) : nodeId(nodeId)
        {
            nodesCount = NumberOfNodes();
            
            n = (ll) GetN();
            maxK = (ll) GetK();
            mod = (ll) GetP();
            
            kPerNode = maxK / nodesCount;
            
            needed = (nodeId == MAIN_NODE_ID) || needWorkers();
            if (!needed)
                return;
            
            if (kPerNode > 0)
            {
                range.start = kPerNode * nodeId + 1;
                if (!isLastNode())
                    range.end = range.start + kPerNode - 1;
                else
                    range.end = maxK; // Cover all remaining Ks with last node
            }
            else
            {
                range.start = 1;
                range.end = maxK;
            }
            
            factorialK = 1;
            numerator = n;
            numeratorMultiplier = n - 1;
            answer = 0;
        }
    
        virtual void act() = 0;
    
    protected:
        int nodesCount, nodeId, kPerNode;
        ll maxK, n, mod;
        Range range;
        ll factorialK, answer, numerator, numeratorMultiplier;
        vector<ll> factorialsK;
        
        void precomputeFactorialsK()
        {
            for (ll i = range.start; i <= range.end; ++i)
            {
                factorialK = (factorialK * i) % mod;
                factorialsK.push_back(factorialK);
            }
        }
        
        void solveSubTask(bool multiply)
        {            
            int idx = 0;
            for (ll k = range.start; k <= range.end; ++k)
            {

                if (multiply)
                    factorialsK[idx] = (factorialsK[idx] * factorialK) % mod;
                
                // Improvements?
                ll inverse = modularPow(factorialsK[idx], mod - 2, mod);

                answer += (numerator * inverse) % mod;
                answer %= mod;
                
                numerator = (numerator * numeratorMultiplier) % mod;
                --numeratorMultiplier;
                ++idx;
            }
            
            if (multiply)
                factorialK = factorialsK.back();
        }
        
        void sendAnswerToNextNode()
        {
            PutLL(nodeId + 1, factorialK);
            PutLL(nodeId + 1, numerator);
            PutLL(nodeId + 1, numeratorMultiplier);
            PutLL(nodeId + 1, answer);
            Send(nodeId + 1);
        }
        
        void printAnswer()
        {
            cout << answer << "\n";
        }
        
        bool needWorkers()
        {
            return kPerNode > 0;
        }
        
        bool isLastNode()
        {
            return nodeId == nodesCount - 1;
        }
};

class MainNode : Node
{
    public:
        MainNode() : Node(MAIN_NODE_ID)
        {
            // Compensate for team with 0 players
            ++answer;
        }
    
        void act() override
        {
            precomputeFactorialsK();
            solveSubTask(false);
            
            if (needWorkers())
                sendAnswerToNextNode();
            else
                printAnswer();
        }
};

class WorkerNode : public Node
{
    public:
        WorkerNode(int nodeId) : Node(nodeId) {}
        
        void act() override
        {
            precomputeFactorialsK();
            receiveAnswerFromPreviousNode();
            solveSubTask(true);
            
            if (isLastNode())
                printAnswer();
            else
                sendAnswerToNextNode();
        }
        
    private:
        void receiveAnswerFromPreviousNode()
        {
            Receive(nodeId - 1);
            factorialK = GetLL(nodeId - 1);
            numerator = GetLL(nodeId - 1);
            numeratorMultiplier = GetLL(nodeId - 1);
            answer += GetLL(nodeId - 1);
        }
};

int main()
{
    ios_base::sync_with_stdio(false);
    
    int nodeId = MyNodeId();
    
    if (nodeId == MAIN_NODE_ID)
    {
        MainNode node;
        node.act();
    }
    else
    {
        WorkerNode node(nodeId);
        if (node.needed)
            node.act();
    }

    return 0;
}