AtCoder Regular Contest 120 AB题
比赛链接:Here
A - Max Add
观察一下发现每次输出与两点有关,前缀和和当前位置最大值
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
ll s = 0, t = 0, mx = INT_MIN;
for (int i = 1, x; i <= n; ++i) {
cin >> x;
s += x, t += s;
mx = max(mx, 1ll * x);
cout << t + 1ll * i * mx << "\n";
}
}
B - Uniformly Distributed
问的是网格里有红色,蓝色,和没涂色的格子,问有多少种方法,将没涂色的格子上色,使得,无论怎么走(题目规定只能向下,向右),使得经过的红色的数量相等。
思路:从必经之路上(斜线)入手,如给斜线上两种颜色都有,那么题目无解,如果仅有一种颜色,那该斜线仅有一种涂色方式,如果都没有,则可涂两种颜色。于是问题的答案变成了 \(2^{cnt}\) ,cnt
代表没有涂色的斜线的条数.
const int N = 510, mod = 998244353;
string s[N];
ll qpow(ll a, ll b) {
ll ans = 1 ;
for (; b; b >>= 1, a = a * a % mod);
if (b & 1) ans = ans * a % mod;
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> s[i], s[i] = "@" + s[i];
int r = 0, b = 0, cnt = 0, ans = 0;
bool f = 0;
for (int i = 1; i <= n; ++i) {
int x = i, y = 1;
r = 0, b = 0, cnt = 0;
while (x >= 1 and y <= m) {
if (s[x][y] == '.') cnt++;
if (s[x][y] == 'R') r = 1;
if (s[x][y] == 'B') b = 1;
x--, y++;
}
if (r == 1 and b == 1)f = 1;
else if (r == 1 and b == 0 || r == 0 and b == 1);
else {
if (cnt) ans++;
}
}
for (int i = 1; i <= m; ++i) {
if (i == 1) continue;
int x = n, y = i;
r = 0, b = 0, cnt = 0;
while (x >= 1 and y <= m) {
if (s[x][y] == '.') cnt++;
if (s[x][y] == 'R') r = 1;
if (s[x][y] == 'B') b = 1;
x--, y++;
}
if (r == 1 and b == 1)f = 1;
else if (r == 1 and b == 0 || r == 0 and b == 1);
else {
if (cnt) ans++;
}
}
if (f) ans = 0;
else ans = (ans + qpow(2, ans)) % mod;
cout << ans << "\n";
}
上面代码写复杂了,看了下其他人的发现一个很简洁的写法
const int md = 998244353;
int n, m, i, j, r;
char s[505][505], c[1010];
void solve() {
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%s", s[i]);
for (j = 0; j < m; j++) if (s[i][j] != '.') {
if (c[i + j] != 0 && c[i + j] != s[i][j]) { puts("0"); return ;}
c[i + j] = s[i][j];
}
}
for (r = 1, i = 0; i <= n + m - 2; i++) if (c[i] == 0) r = (r * 2) % md;
printf("%d\n", r);
}