#P15463. 【MX-X25-T7】『FeOI-5』三角晶体
【MX-X25-T7】『FeOI-5』三角晶体
Problem Description
At the beginning, there is a triangle. Every second, a new point appears, and it connects with equal probability to two adjacent points on the current outermost boundary of the shape.
All points are labeled. All edges are undirected, and each edge has weight .

Given , for each , compute the expected value of the sum of shortest-path lengths between all pairs of points after the shape has grown to have exactly points, and take it modulo the given prime .
Formal statement:
On an infinite 2D plane, initially there is a triangle made of three undirected edges, i.e. there are points and edges . Then at the -th second:
- Uniformly at random choose an undirected edge on the outer boundary of the shape (the boundary that is in the same face as infinity).
- Create a point on the outside of this edge (in the face containing infinity).
- Add undirected edges and .
Given , for each , compute the expected value of the sum of shortest-path lengths over all unordered pairs of points after seconds, and output it modulo the input prime .
Input Format
The first line contains two integers .
Output Format
To avoid an excessively large output, output one line with a single integer: the XOR of all answers for .
4 998244353
7
10 1000000007
67634987
Hint
[Sample 1 Explanation]
At this time, there are three possible cases:

It is easy to see that in each case the total sum of shortest paths is , so the expectation is .
[Sample 2 Explanation]
The answers for from to are:
7
13
800000027
800000038
190476238
138095302
124338708
[Constraints]
For all testdata, , , and it is guaranteed that is prime.
| Subtask ID | Score | |
|---|---|---|
[Hint]
Contestants are advised to use the fast modulo template used in the std solution to reduce constant factors:
typedef __int128_t lll;
typedef unsigned long long ull;
struct FastMod{ull b, m;
inline void init(ull x){b = x, m = ((lll)1 << 64) / b;}
inline ull Mod(ull a){
ull q = ull((lll)m * a >> 64);
ull r = a - q * b;
return r >= b ? r - b : r;
}
}md;
Translated by ChatGPT 5