#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 11.

Given nn, for each k∈[4,n]k \in [4, n], compute the expected value of the sum of shortest-path lengths between all pairs of points after the shape has grown to have exactly kk points, and take it modulo the given prime pp.

Formal statement:

On an infinite 2D plane, initially there is a triangle made of three undirected edges, i.e. there are points 1,2,31, 2, 3 and edges (1,2),(2,3),(3,1)(1,2), (2,3), (3,1). Then at the ii-th second:

  • Uniformly at random choose an undirected edge (u,v)(u, v) on the outer boundary of the shape (the boundary that is in the same face as infinity).
  • Create a point i+3i+3 on the outside of this edge (in the face containing infinity).
  • Add undirected edges (u,i+3)(u, i+3) and (v,i+3)(v, i+3).

Given nn, for each k∈[4,n]k \in [4, n], compute the expected value of the sum of shortest-path lengths over all unordered pairs of points after k−3k-3 seconds, and output it modulo the input prime pp.

Input Format

The first line contains two integers n,pn, p.

Output Format

To avoid an excessively large output, output one line with a single integer: the XOR of all answers for k∈[4,n]k \in [4, n].

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 1+1+1+1+1+2=71+1+1+1+1+2=7, so the expectation is 77.

[Sample 2 Explanation]

The answers for kk from 44 to 1010 are:

7
13
800000027
800000038
190476238
138095302
124338708

[Constraints]

For all testdata, 4≤n≤1064 \le n \le 10^6, 1145143≤p≤109+91145143 \le p \le 10^9+9, and it is guaranteed that pp is prime.

Subtask ID nn Score
11 =20=20 55
22 =100=100 1010
33 =1000=1000 3030
44 =106=10^6 5555

[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