CF101E Candies and Stones

题目描述

小杰拉尔德和他的教练迈克玩一个有趣的游戏。 游戏开始时,有 $n$ 个糖果和 $m$ 块石头。杰拉尔德和迈克轮流行动,迈克先行动。 - 迈克行动时,他会检查杰拉尔德吃了多少糖果和石头。如果杰拉尔德吃了 $a$ 块糖和 $b$ 块石头,他就会得到 $f(a, b)$ 奖分。其中 $f(a, b) = (x_a + y_b) \bmod p$。 - 杰拉尔德行动时,他要么从糖果堆里吃掉一块糖果,要么从石头堆里吃掉一块石头。 当迈克看到杰拉尔德把除了一块糖和一块石头之外的糖果和石头都吃光时,他最后一次得分,游戏结束。迈克不允许杰拉尔德吃所有的糖果,也不允许他吃所有的石头。编程求出杰拉尔德如何游戏才能获得最大的分数,并求出一组方案。

输入格式

第一行三个整数 $n, m, p$($1\leq n, m\leq 20000$,$1 \leq p\leq 10^9$)。 第二行 $n$ 个整数 $x_0, x_1, \cdots, x_{n - 1}$。 第三行 $m$ 个整数 $y_0, y_1, \cdots, y_{m - 1}$。

输出格式

第一行一个整数表示杰拉尔德能得到的最大分数。 第二行 $n + m - 2$ 个字符,其中每一个都是 `C` 或 `S`;如果第 $i$ 个字符是 `C`,表示得到最大分数情况下杰拉尔德第 $i$ 次行动应该选择吃糖果;如果第 $i$ 个字符是 `S`,表示得到最大分数情况下杰拉尔德第 $i$ 次行动应该选择吃石头。如果有多种可能的方案,输出任意一种。 Translated by Lolierl

说明/提示

In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get $ 0 $ points before his first move, and $ 1 $ after his second one. This, the maximum number of points Gerald can get equals to $ 2 $ , and for that he should first eat a stone, then a candy.