Restaurant Recommendation Rescue

시간 제한2초메모리 제한2048 MB

요약
배열 B가 주어지고 원소 교환이 여러 번 일어날 때, K의 추천 알고리즘이 만들 수 있는 배열 A와 일치하는 모든 순환 시프트 k의 개수와 합을 각 단계마다 구한다.
난이도

어려움10점 중 9점

유형
문자열 매칭, 조합론, 수학, 트리
정답자
아직 제출이 없습니다

문제

A certain aspiring musician K loves going for shabu-shabu! Recently, she’s been to NN shabushabu restaurants, numbered 1,2,…,N1, 2, \dots , N, following the following algorithm:

  1. K keeps an ordered list of recommendations, starting with restaurant 11.
  2. On the ii-th day, she visits the next recommended restaurant on her list, which recommends her restaurants R_i=r_i,1,…,r_i,ℓ_iR\_i = \\{r\_{i,1}, \dots , r\_{i,ℓ\_i}\\}.
  3. K appends R_iR\_i to her list of restaurants to visit.
  4. K repeats steps 2-4 until she runs out of recommended restaurants.
  5. K writes down the array A_0,…,A_N−1A\_0, \dots , A\_{N−1}, where A_iA\_i equals the number of restaurants she was recommended on the (i+1)(i + 1)-th day. That is, A_i=∣R_i+1∣A\_i = |R\_{i+1}|.

It is guaranteed that ⋃N_i=1R_i=2,…,N\bigcup^N\_{i=1} R\_i = \\{2, \dots , N\\} and R_i∩R_j=∅R\_i ∩ R\_j = ∅ for i≠ji \ne j, that is, every restaurant, other than the first, will be recommended by exactly one other restaurant.

Once K finishes her list, K’s delinquent friend H decides to play a prank on her! She replaces the array A_0,…,A_N−1A\_0, \dots , A\_{N−1} with another array B_0,…,B_N−1B\_0, \dots , B\_{N−1}! K thinks that this new array B_iB\_i might just be a cyclic shift of her array, so she asks you to determine all possible 0≤k<N0 ≤ k < N such that A_i=B_(i+k) mod NA\_i = B\_{(i+k) \bmod N}, for all 0≤i<N0 ≤ i < N and any valid output of her algorithm A_0,…,A_N−1A\_0, \dots , A\_{N−1}.

Furthermore, K will then perform QQ operations, where for the ii-th operation, she swaps B_x_iB\_{x\_i}, B_y_iB\_{y\_i} and asks you to do the same on the new array. Can you help K see through her friend’s prank?

입력

The first line of input will contain two integers, NN (1≤N≤500,0001 ≤ N ≤ 500\\, 000) and QQ (0≤Q≤300,0000 ≤ Q ≤ 300\\, 000).

The next line of input will contain NN space-separated non-negative integers, B_0,B_1,…,B_N−1B\_0, B\_1, \dots , B\_{N−1} (0≤B_i<N0 ≤ B\_i < N), the initial sequence.

The ii-th of the next QQ lines of input will contain two integers each, x_ix\_i and y_iy\_i (0≤x_i,y_i<N0 ≤ x\_i , y\_i < N and x_i≠y_ix\_i \ne y\_i), indicating you are to swap B_x_iB\_{x\_i} with B_y_iB\_{y\_i} .

출력

For each of the Q+1Q + 1 arrays (including the initial array B_0,…,B_N−1B\_0, \dots , B\_{N−1}), let S=k_1,…,k_mS = \\{k\_1, \dots , k\_m\\} denote the set of integers 0≤k_j<N0 ≤ k\_j < N such that there exists a valid output A_0,…,A_N−1A\_0, \dots , A\_{N−1} of K’s algorithm such that A_i=B_(i+k_j) mod NA\_i = B\_{(i+k\_j ) \bmod N} for all 0≤i<N0 ≤ i < N. Output, on a single line, the integers mm and ∑m_i=1k_i(mod998,244,353)\sum^m\_{i=1} k\_i \pmod {998\\, 244\\, 353}, separated by a space.

In particular, if S=∅S = ∅, your output should be 0 0.

예제1

  1. 예제 1

    입력
    5 3
    1 2 0 0 1
    0 2
    1 3
    3 2
    
    예상 출력
    1 4
    1 1
    1 2
    1 2