10.20提高组模拟赛

· · 个人记录

乘法破译(multiplication)

观察表格和题目给定的A=3,B=0,C=2,D=1的值,就可以发现除了0和1以外,每行的字母的每项的首字母不同的数量-1=每行的字母

正确代码

#include <bits/stdc++.h>
using namespace std;
bool cnt[5005], vis[5005];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int ans = 0;
        for (int j = 0; j < n; ++j) {
            vis[j] = cnt[j] = 0;
        }
        for (int j = 1; j <= n; ++j) {
            int a, b;
            cin >> a >> b;
            vis[a] = vis[b] = 1;
            if (!cnt[a]) {
                ans++;
                cnt[a] = 1;
            }
        }
        int sum = 0;
        for (int j = 0; j < n; ++j) {
            if (vis[j]) sum++;
        }
        if (ans == 1) {
            if (sum == 4) cout << 1 << ' ';
            else cout << 0 << ' ';
        }
        else cout << ans << ' ';
    }
    return 0;
}

序列异或(xor)

因为 a^b=c^d,所以可以固定a和b的异或值,每次前i个统计a和b的异或的次数,根据题目,c和d都在a和b的后面,那么枚举i+1~n,答案加上异或的次数。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5005, M = 1000005;
int a[N];
int cnt[2 * M];
int n;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
//  freopen("xor.in", "r", stdin);
//  freopen("xor.out", "w", stdout);
    cin >> n; 
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = i + 1; j <= n; ++j) {
            ans += cnt[a[i] ^ a[j]];
        }
        for (int j = 1; j < i; ++j) cnt[a[i] ^ a[j]]++;
    }
    cout << ans;
    return 0;
}

矩阵删除(matrix)

依题意得,每个位置i,j上一行删除的区间为[max(0, j - k), min(m, j + k)],那么i,j加上[max(0, j - k), min(m, j + k)]的答案数,可以用前缀和维护。注意:还会有相同的,以为相同的只会在i,j-1和i,j出现,那么需减去数量,还可疑用浅醉和维护。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3005, mod = 1e9 + 7;
int dp[N][N], same[N][N], s1[N][N], s2[N][N];
char c[N][N];
int n, m, k;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) cin >> c[i][j];
    }
    for (int i = 1; i <= m; ++i) {
        dp[1][i] = 1;
        same[1][i] = (i != 1 && c[1][i] == c[1][i - 1]);
        s1[1][i] = (s1[1][i - 1] + dp[1][i]) % mod;
        s2[1][i] = (s2[1][i - 1] + same[1][i]) % mod;
    }
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            dp[i][j] = ((s1[i - 1][min(m, j + k)] - s1[i - 1][max(0ll, j - k - 1)]) + mod - (s2[i - 1][min(m, j + k)] - s2[i - 1][max(0ll, j - k)] + mod)) % mod;
            if (j == 1 || c[i][j - 1] != c[i][j]) {
            }
            else {
                same[i][j] = ((s1[i - 1][min(m, j + k - 1)] - s1[i - 1][max(0ll, j - k - 1)] + mod) - (s2[i - 1][min(m, j + k - 1)] - s2[i - 1][max(0ll, j - k)] + mod) + mod) % mod;
            }
            s1[i][j] = (s1[i][j - 1] + dp[i][j]) % mod;
            s2[i][j] = (s2[i][j - 1] + same[i][j]) % mod;
        }
    }
    long long ans = 0;
    for (int i = 1; i <= m; ++i) {
        ans = (ans + (dp[n][i] - same[n][i] + mod) % mod) % mod;
    }
    cout << ans;
    return 0;
}

https://share.note.youdao.com/s/ZHq7FM1T