A+B Problem
题解:
分两种情况
- 越界
可以发现|a| + |b| + |ans| == 2048
那么如何搞这个呢
1.上越界,循环到负数,把他搞到上越界的部分的数,搞成正数,那么外围减去 1024 1024 1024
2.下越界,循环到正数,把他搞到下越界的部分的数,搞成正数 + 1024 + 1024 +1024,外围减去 1024 1024 1024
就可以推出来了 - 不越界
正常算
内外 1024 1024 1024相互抵消
AC
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
int t;
cin >> t;
while (t -- ) {
int a, b;
cin >> a >> b;
cout << (a + b + 1024 + 2048) % 2048 - 1024 << endl;
}
return 0;
}
Game
题解
如果只有 − 9 -9 −9 操作,那么可以把这些数按 9 9 9 的余数取模,对于 − 99 , − 999 , − 9999..... -99, -999, -9999..... −99,−999,−9999..... 都是一样的,奇偶性不变,那么为了让数不一样,我们就都减去 9 9 9 ,然后再排序,如果取模后的余数相同的数只有一个就OK,如果不是,那我们就逆推,我们可以最多模几个 9 9 9
伪代码举例:
i.e.
2, 11, 20, 29
i.e.
2, 2, 2, 2
ans = 1 + 2 + 3;
sum[2] = 4;
ans -= 4 * (4 - 1) / 2;
ans = 0;
cout << "B" << endl;
AC
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N];
int sum[10];
int main() {
int t;
cin >> t;
while (t -- ) {
int n;
cin >> n;
LL ans = 0;
memset(sum, 0, sizeof sum);
memset(a, 0, sizeof a);
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
ans += a[i] / 9;
a[i] %= 9;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i ++ ) {
sum[a[i]] ++;
}
for (int i = 1; i < 9; i ++ ) {
ans -= sum[i] * (sum[i] - 1) / 2;
}
ans -= sum[0] * (sum[0] + 1) / 2;
if (ans % 2 == 1) cout << "A" << endl;
else cout << "B" << endl;
}
return 0;
}
Substring
题解
滑窗,从左到右滑过去就好
AC
#include <bits/stdc++.h>
using namespace std;
int sum[30];
int ops[30];
int main() {
int t;
cin >> t;
while (t -- ) {
int k;
cin >> k;
string str;
memset(sum, 0, sizeof sum);
memset(ops, 0, sizeof ops);
cin >> str;
int len = str.size();
int ans = 0;
int i, j;
for (i = 0, j = 0; i < len; i ++ ) {
if (sum[str[i] - 'a'] < k) {
if (sum[str[i] - 'a'] == 1)
ops[str[i] - 'a'] = i;
sum[str[i] - 'a'] ++;
} else {
if (k != 1) {
j = ops[str[i] - 'a'];
}
else j = i;
}
ans = max(i - j + 1, ans);
}
ans = max(ans, i - j);
cout << ans << endl;
}
return 0;
}