A — Q老师与石头剪刀布
题目描述
每一个大人曾经都是一个小孩,Q老师 也一样。
为了回忆童年,Q老师 和 Monika 玩起了石头剪刀布的游戏,游戏一共 n 轮。无所不知的 Q老师 知道每一轮 Monika 的出招,然而作为限制, Q老师 在这 n 轮游戏中必须恰好出 a 次石头,b 次布和 c 次剪刀。
如果 Q老师 赢了 Monika n/2(上取整) 次,那么 Q老师就赢得了这场游戏,否则 Q老师 就输啦!
Q老师非常想赢,他想知道能否可以赢得这场游戏,如果可以的话,Q老师希望你能告诉他一种可以赢的出招顺序,任意一种都可以。
Input
第一行一个整数 t(1 ≤ t ≤ 100)表示测试数据组数。然后接下来的 t 组数据,每一组都有三个整数:
第一行一个整数 n(1 ≤ n ≤ 100)
第二行包含三个整数 a, b, c(0 ≤ a, b, c ≤ n)。保证 a+b+c=n
第三行包含一个长度为 n 的字符串 s,字符串 s 由且仅由 ‘R’, ‘P’, ‘S’ 这三个字母组成。第 i 个字母 s[i] 表示 Monika 在第 i 轮的出招。字母 ‘R’ 表示石头,字母 ‘P’ 表示布,字母 ‘S’ 表示剪刀
Output
对于每组数据:
如果 Q老师 不能赢,则在第一行输出 “NO”(不含引号)
否则在第一行输出 “YES”(不含引号),在第二行输出 Q老师 的出招序列 t。要求 t 的长度为 n 且仅由 ‘R’, ‘P’, ‘S’ 这三个字母构成。t 中需要正好包含 a 个 ‘R’,b 个 ‘P’ 和 c 个 ‘S’
“YES”/"NO"是大小写不敏感的,但是 ‘R’, ‘P’, ‘S’ 是大小写敏感的。
Sample Input
2
3
1 1 1
RPS
3
3 0 0
RPS
Sample Output
YES
PSR
NO
解题思路
定义两个char数组,一个存储Monika的出招顺序,另一个存储Q老师的出招顺序;
从Monika 在第1轮的出招开始一直遍历,当有赢的条件的时候就出能赢的,对应的数量减一;如果不能赢,那么就将对应的出招置为‘o’;
输出Q老师的出招:
数组中不等于‘o’的直接输出,等于‘o’的则看a, b, c中那个不为0,直接输出即可;
解题代码
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
double n, win;
int t, a, b, c;
char d[105], ch[105];
void judge() {
win = 0;
for (int y = 0; y < n; y++) {
if (d[y] == 'R') {
if (b != 0) {ch[y] = 'P'; b--; win++;}
else ch[y] = 'o';
}
else if (d[y] == 'P') {
if (c != 0) {ch[y] = 'S'; c--; win++;}
else ch[y] = 'o';
}
else {
if (a != 0) {ch[y] = 'R'; a--; win++;}
else ch[y] = 'o';
}
}
}
int main() {
cin>>t;
for (int x = 0; x < t; x++) {
cin>>n;
memset(d, 0, sizeof(d));
memset(ch, 0, sizeof(ch));
cin>>a>>b>>c;
for (int y = 0; y < n; y++) {
char z; cin>>z;
d[y] = z;
}
judge();
if (win >= ceil(n / 2.0)) {
cout<<"YES"<<endl;
for (int y = 0; y < n; y++) {
if (ch[y] != 'o') cout<<ch[y];
else {
if (a != 0) {cout<<'R'; a--;}
else if (b != 0) {cout<<'P'; b--;}
else if (c != 0) {cout<<'S'; c--;}
}
}
cout<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}
B
题目描述
Q老师 得到一张 n 行 m 列的网格图,上面每一个格子要么是白色的要么是黑色的。
Q老师认为失去了 十字叉 的网格图莫得灵魂. 一个十字叉可以用一个数对 x 和 y 来表示, 其中 1 ≤ x ≤ n 并且 1 ≤ y ≤ m, 满足在第 x 行中的所有格子以及在第 y 列的 所有格子都是黑色的
例如下面这5个网格图里都包含十字叉:
第四个图有四个十字叉,分别在 (1, 3), (1, 5), (3, 3) 和 (3, 5).
下面的图里没有十字叉:
Q老师得到了一桶黑颜料,他想为这个网格图注入灵魂。 Q老师 每分钟可以选择一个白色的格子并且把它涂黑。现在他想知道要完成这个工作,最少需要几分钟?
Input
第一行包含一个整数 q (1 ≤ q ≤ 5 * 10^4) — 表示测试组数
对于每组数据:
第一行有两个整数 n 和 m (1 ≤ n, m ≤ 5 * 10^4, n * m ≤ 4 * 10^5) — 表示网格图的行数和列数
接下来的 n 行中每一行包含 m 个字符 — ‘.’ 表示这个格子是白色的, ‘*’ 表示这个格子是黑色的
保证 q 组数据中 n 的总和不超过 5 * 10^4, n*m 的总和不超过 4 * 10^5
Output
答案输出 q 行, 第 i 行包含一个整数 — 表示第 i 组数据的答案
Sample Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Sample Output
0
0
0
0
0
4
1
1
2
解题思路
用一个char类型的二维数组map存储所给图的信息,int类型的x、y数组存储对应的行、列上白色区域的数量,在输入过程中,假如map[a][b]输入的字符是‘ . ‘ 那么对应的x[a] y[b]++;
对图进行遍历,遍历到的这一块不论黑白,要想变成一个十字叉,那么目标时间就是x、y中对应坐标的值的和,假如这一块是白色的,那么其在横、纵坐标上被上色了两次,所以需要减一,ans存储的就是所有情况中值最小的那个,最后输出即可;
解题代码
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int q, m, n;
int main() {
cin>>q;
while (q--) {
cin>>n>>m;
char map[n+5][m+5];
int x[50010], y[50010];
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= m; b++) {
char c; cin>>c; map[a][b] = c;
if (map[a][b] == '.') {
x[a]++; y[b]++;
}
}
}
int ans = 9999999;
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= m; b++) {
int mid = x[a] + y[b];
if (map[a][b] == '.') mid--;
if (ans > mid) ans = mid;
}
}
cout<<ans<<endl;
}
}
C
题目描述
Q老师 对数列有一种非同一般的热爱,尤其是优美的斐波那契数列。
这一天,Q老师 为了增强大家对于斐波那契数列的理解,决定在斐波那契的基础上创建一个新的数列 f(x) 来考一考大家。数列 f(x) 定义如下:
当 x < 10 时,f(x) = x;
当 x ≥ 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10),ai 只能为 0 或 1。
Q老师 将给定 a0~a9,以及两个正整数 k m,询问 f(k) % m 的数值大小。
聪明的你能通过 Q老师 的考验吗?
Input
输入文件包含多组测试用例,每组测试用例格式如下:
第一行给定两个正整数 k m。(k < 2e9, m < 1e5)
第二行给定十个整数,分别表示 a0~a9。
Output
对于每一组测试用例输出一行,表示 f(k) % m 的数值大小。
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
解题思路
用矩阵快速幂得到(k - 9)的新矩阵,然后最后求得f(k) % m即为答案
解题代码
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int n = 10;
long long int k, m;
struct Matrix {
long long int x[n][n];
Matrix operator* (const Matrix& b) const{
Matrix ret;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ret.x[i][j] = 0;
for (int k = 0; k < n; k++) {
ret.x[i][j] += (x[i][k] * b.x[k][j]) % m;
ret.x[i][j] %= m;
}
}
}
return ret;
}
Matrix() {memset(x, 0, sizeof(x));}
Matrix(const Matrix& b) {memcpy(x, b.x, sizeof(x));}
};
Matrix quick_pow(Matrix a, long long int x) {
Matrix ret;
for (int i = 0; i < n; i++) ret.x[i][i] = 1;
while (x) {
if (x & 1) ret = ret * a;
a = a * a;
x >>= 1;
}
return ret;
}
int main() {
while (cin>>k>>m) {
Matrix ma;
for(int i = 0; i < 10; i++) cin>>ma.x[0][i];
for(int i = 1; i < 10; i++) ma.x[i][i-1] = 1;
Matrix tmp = quick_pow(ma, k - 9);
long long int ans = 0;
for(int i = 0; i < 10; i++) {
ans += tmp.x[0][i] * (9 - i);
ans = ans % m;
}
cout<<ans<<'\n';
}
return 0;
}