P10103: 糖果和石头
传统题
2.000s
时间限制
256MB
内存限制
1 提交
0 解决
【题目描述】
【
题目描述】
小杰拉尔德和他的教练麦克玩了一个有趣的游戏。在游戏开始时,有一堆n
个糖果和一堆m个石头。杰拉尔德和迈克轮流走,迈克先走。在移动
的过程中,麦克检查杰拉尔德吃了多少糖果和石头。杰拉尔德每
吃a块糖果和b块石头。麦克就
奖励杰拉尔德 f(a,b)
个积分。在移动过程中,杰拉尔德要么从糖果堆里吃一颗糖果,要么从石头堆里吃一块石头。当
迈克看到杰拉尔
德除了一块糖果和一块石头外,什么都吃光了,于是他最后一次给他打分,游戏结束了。杰拉尔德不允许吃所有的糖果,也不允许吃所有的石头。告诉杰拉尔德如何玩才能得到尽可能多的分
数:
需要找到杰拉德可能的最优策略之一。
【
输入】
第一行包含三个整数n
、m、p(1≤n,m≤20000,1≤p≤109)。第二行包含n个整数x0, x1,…, xn-1(0≤xi≤20000)。第三行包含m个整数y0, y1,…, m-1(0≤yi≤20000)。f(a,b)的值是xa+yb除以p的余数。
【
输出】
在第一行输出
唯一的数字:
杰拉德可以获得的最大分数
。在第二行输出
一个由n+m-2
个字符组成的字符串,每个字符要么是“C”,要么是“S”,如果杰拉尔德的第i步应该吃糖果,那么第i个字符应该是“C”,如果他应该吃石头,那么第S个字符应该是“S”。
【
输入样例一】
2 2 10
0 0
0 1
【
输出样例一】
2
SC
【
输入样例二】
3 3 10
0 2 0
0 0 2
【
输出样例二】
10
CSSC
【
输入样例三】
3 3 2
0 1 1
1 1 0
【
输出样例三】
4
SCSC
【样例说明】
在第一个测试中,如果杰拉尔德的第一个动作是吃石头,他将得到一分,如果他吃了糖果,他将得到零分。无论如何,杰拉尔德在第一次行动前得0
分,第二次行动后得1分。杰拉尔德能得到的最高分是2分,为此他应该先吃一块石头,然后再吃一块糖果。
【提示】
Output
Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of
n+m-2 characters, each of which is either a "
C" or "
S", the
i-th character should be "
C" if Gerald's
i-th move should be eating a candy and "
S" if he should eat a stone.
Note
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.