2025.07.12课堂总结

· · 个人记录

2025.07.12课堂总结

重点一

再次复习了前缀和答案这一知识点
例题(P3131 [USACO16JAN] Subsequences Summing to Sevens S):

#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 5e5 + 5;
int n, a[N], sum[N], pre[N];
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i = 0; i <= n; i++) pre[i] = -1;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    }
    int maxx = 0;
    for(int i = 0; i <= n; i++){
        if(pre[sum[i] % 7] == -1){
            pre[sum[i] % 7] = i;
        }
        maxx = max(maxx, i - pre[sum[i] % 7]);
    } 
    cout << maxx;
    return 0;
}

例题(Number of Ways):

#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 5e5 + 5;
int n, a[N], sum[N];
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    } 
    if(sum[n] % 3 != 0){
        cout << 0 << endl;
        return 0; 
    }
    int cnt = 0, res = 0, x = sum[n] / 3;
    for(int i = 1; i <= n; i++){
        if(sum[i] == x * 2 && i > 1 && i < n){
            cnt += res; 
        }
        if(sum[i] == x) res++;
    }
    cout << cnt;
    return 0;
}

重点二

讲解了模这一操作的性质和一些公式,如:

(a + b) % c = (a % c + b % c) % c   
(a - b) % c = ((a % c - b % c)% c + c) % c   
(a * b) % c = ((a % c) * (b % c)) % c   
(a / b) % c != (a % c / b % c) % c

例题([蓝桥杯 2017 省 B] k 倍区间):

#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 1e5 + 5;
int n, a[N], tong[N], sum[N], k, ans;
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin >> n >> k;
    tong[sum[0]] = 1;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++){
        sum[i] = (sum[i - 1] + a[i]) % k;
        ans += tong[sum[i]];
        tong[sum[i]]++;
    }
    cout << ans;
    return 0;
}

好了,今天的总结就写到这了,感谢你的观看😊😊😊