Description
给定一个序列 (a_i),(iin[0,n))。两个人 (A) 和 (B) 玩游戏,每次 (A) 可以生成一个序列 (b_i),然后 (B) 选择一个数 (x),使 (a_i) 加上 (b_{(i+x)mod n})。如果所有的 (a_i) 都是一个质数 (p) 的倍数,游戏结束。
Solution
所有 (a) 的运算在(mod p) 意义下进行,下标运算在 (mod n) 意义下进行。
博弈论正难则反,先考虑终止情况。先考虑 0 轮就结束的情况,那就是全都是 0。若第一轮就结束,那就是所有 (a_i) 都相同。那第二轮就结束呢?那就是无论怎么加都会转移到第一轮就结束的情况。列出式子,就是说
[forall i,jin[0,n-1], a_i+b_j=a_{i+1}+b_{j+1}
]
一观察,发现就是说 (a) 的一阶差分要全部相等。一归纳,就发现,在 (n) 轮结束的充分必要条件就是 (a) 的 (n) 阶差分序列全为 (0)。那就得到一个 (O(n^2)) 的做法,直接做差分,直到为 (0)。
因为运算在模 (p) 意义下进行。这里有一个高阶差分的技巧。考虑一种 (p^s) 阶差分的式子
[Delta^{p^s} a_i=sum_{k=0}^{p^s} (-1)^k inom{p^s}{k} a_{i-k}
]
因为 (forall kin(0,p^s)),(inom{p^s}{k}equiv 0 pmod p),可以考虑 Lucas 展开证明。式子就变成了
[Delta^{p^s} a_i=a_i-a_{i-p^s}
]
(中间的减号:当 (p>2) 时,((-1)^{p^s}=-1);当 (p=2),加减号无所谓,只与奇偶性有关)
这样就可以考虑 (p) 进制倍增。复杂度 (O(nlog_p n))。