## A. Completing the square

**Solution**

```
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
cout<<n*n<<endl;
}
```

## B. Fireworks

**Hint**

What is the maximum possible value of $$$d(M)$$$.

**Solution**

We can see that for $$$ M \le 10^18$$$, the maximum possible value of $$$d(M)$$$ is only $$$ 9 * 18$$$ attained for $$$999999999999999999$$$. So for finding the number of solutions for a given $$$N$$$, we can iterate over all the values of $$$d(M)$$$ upto $$$9 *18$$$, and then check if $$$d(N/p) == p$$$.

## C. Lights

**Hint**

Try solving the easier version of the problem where we only need to find the number of distinct simple paths in DAG.

**Solution**

In this problem, we need to find the number of distinct simple paths in DAG, which start at a vertex with indegree 0 and end on a node with out-degree 0. To solve this problem, we first do topo-sort on the graph to get the ordering, then initialize a dp value of 0 for all nodes. For the nodes with outdegree 0, set the value of dp to 1. Then while iterating over all nodes $$$u$$$, in their topological order, update the dp value with the sum of dp value of all $$$v$$$ such that there exists an edge $$$(u,v)$$$ in the graph. In the end, print the sum of dp value of all nodes with indegree 0.

## D. Dazzling Sequences

**Hint 1**

Solve the problem when $$$ M = 0$$$.

**Hint 2**

Build a graph with $$$K$$$ vertex, labelled from $$$0, 1, 2, ..., K -1 $$$. For each $$$i$$$ in range $$${1..P}$$$, add an edge from $$$j$$$ to $$$j + i$$$ for all j in range $$${0..K-1}$$$. Count the number of paths from $$$0$$$ to $$$0$$$ of length $$$N$$$.

**Solution**

First, we will try to solve the problem for the case where $$$M=0$$$. For doing this, build a graph with $$$K$$$ vertex, labelled from $$$0, 1, 2, ..., K -1 $$$. For each $$$i$$$ in range $$${1..P}$$$, add an edge from $$$j$$$ to $$$j + i$$$ for all j in range $$${0..K-1}$$$. Count the number of paths from $$$0$$$ to $$$0$$$ of length $$$N$$$. To do this we can define

The transition can we simply be obtained by looking at the graph, which says that

To optimize this for large N, we can see that the transitions are independent of $$$i$$$ hence we can use matrix exponentiation to find the answer

For solving the problem when $$$M!=0$$$, first find the solution that assumes there is no constraint, then we will subtract the number of solution which do not contains any of these $$$M$$$ numbers, after subtracting these two answers we will get the required value. For computing the case which contains only these $$$M$$$ numbers, we will build the graph and thus the matrix with transition using only these M values.

## E. Colourful Diyas

**Hint**

Try calculating the number of ways to have happiness $$$\geq i$$$.

**sub-Hint**

Overcounting? Maybe let it remain. Try connecting the result to the number of ways to have happiness exactly $$$i$$$.

**Solution**

Note that the happiness cannot exceed $$$m$$$ so we will use it as the upper bound in all summations below.

Let $$$G_i$$$ denote the number of ways to have happiness $$$\geq i$$$. One way to guarantee that we have at least $$$i$$$ happiness is to choose $$$i$$$ colours and their $$$k*i$$$ places, and fill all remaining places randomly with the non-chosen $$$m - i$$$ colours. The number of ways to do so is:

Unfortunately, this is wrong and it overcounts. Specifically, for $$$j \geq i$$$, $$$G_i$$$ counts each way with happiness $$$j$$$, $$$\binom{j}{i}$$$ times. (Try to see why?).

If we let $$$E_i$$$ denote the way to get happiness exactly $$$i$$$, this leads us to the following relation between the "incorrect" values of $$$G_i$$$ and $$$E_i$$$.

Using the above relation and induction, we can show that:

Therefore,

This expression can now be evaluated for all $$$i$$$ using FFT with coefficients as the terms in the two brackets in the final summation.

## F. The Evil Company

**Hint 1**

Simplify the problem first by finding the sum of edge weights from 1 to each node. Then replace socket cost by socket cost — sum of edge weight from 1. and switch cost by switch cost + sum of edge weight from 1.

**Solution**

If we make the reduction as shown above the problem reduces to a matching problem, when given some nodes, we have to find the matching with the maximum sum of nodes, such that a node can be paired only with nodes in its subtree.