跳到正文

个人记录

2025.07.14周考总结

huyunshen约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年7月14日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v1当前

2025.07.14周考总结

今天班上举行了“周考”。同学们都开心极了,这次呢大家都还考的不错吧,借此呢我也总结了一下自己的不足。好了,废话不多说,总结就从这开始吧!

声明:在之前课堂上讲过,且AC的题目不做另行分析

P1147 连续自然数和

这题别看有多难,实际上O(n2)O(n^2)枚举加可行性剪枝即可完成。唯一注意的是要怎么写才能做到答案正确且剪枝正确。
代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int N = 2e6 + 3;
ll sum[N], n;
int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		sum[i] = sum[i - 1] + i;
	} 
	for(int l = 1; l <= n; l++){
		for(int r = l + 1; r <= n; r++){
			if(sum[r] - sum[l - 1] == n){
				cout <<sum[l] - sum[l - 1] <<' '<<sum[r] - sum[r - 1]<<endl;
				break;
			}
			if(sum[r] - sum[l - 1] > n) break;
		}
	}
	return 0;
}

P8649 [蓝桥杯 2017 省 B] k 倍区间

这道题就是用桶+前缀和+同余定理写的题,(MD,我没写出来) 。硬生生把O(n2)O(n^2)砍成O(n)O(n),于是便可AC。 代码:

C++
#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;
}

P10233 [yLCPC2024]dx 分计算

把dx分使用前缀存起来再求区间dx分即可,没什么难度。

C++
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
string s;
const int N = 1e7 + 3;
int q, sum[N]; 
int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int T;
	cin >> T;
	while(T--){
		cin >> s >> q;
		int len = s.size();
		for(int i = 1; i <= len; i++){
			if(s[i - 1] == 'P'){
				sum[i] = sum[i - 1] + 3;
			}else if(s[i - 1] == 'p'){
				sum[i] = sum[i - 1] + 2;
			}else if(s[i - 1] == 'G'){
				sum[i] = sum[i - 1] + 1;
			}else{
				sum[i] = sum[i - 1];
			}
		}
		for(int i = 1; i <= q; i++){
			int l, r;
			cin >> l >> r;
			cout << sum[r] - sum[l - 1]<<endl;
		}
		for(int i = 1; i <= len; i++) sum[i] = 0;
	}
	return 0;
}

P8772 [蓝桥杯 2022 省 A] 求和

这道题表面上看需要暴力,但是经过数学分析后,我们不难发现:

a1⋅a2+a1⋅a3+⋯+a1⋅an+a2⋅a3+⋯+an−2⋅an−1+an−2⋅an+an−1⋅an=a1⋅(a2+a3+a4+⋅⋅⋅)+a2⋅(a3+a4+a5+⋅⋅⋅)+⋅⋅⋅a_1 ⋅a_2 +a_1 ⋅a_3 +⋯+a_1 ⋅a_n +a_2 ⋅a_3 +⋯+a_{n-2} ⋅a_{n-1} +a_{n-2} ⋅a_n +a_{n-1} ⋅a_n =a_1 ⋅(a_2 + a_3 + a_4 + ⋅⋅⋅) + a_2 ⋅(a_3 + a_4 + a_5 + ⋅⋅⋅) + ⋅⋅⋅

而推理出的公式后的括号恰好可以用前缀答案储存,所以题目只要推理出这个利于前缀答案的公式即可。
代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int N = 2e5 + 5;
ll a[N], ans, cnt, n, sum[N];
int 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];
	}
	for(int i = 1; i <= n; i++){
		ans += a[i] * (sum[n] - sum[i]);
	}
	cout << ans;
	return 0;
}

U512656 数字选择3

这道题的本质是:给定nn个数字,满足区间和最大,区间和Mod 100的答案。再观察sum的规律,发现:枚举r即可,范围总是0~99。这样写时间复杂度便降下来了。
代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int N = 1e6 + 5;
int n, a[N], sum[N], tong[N];
int 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];
        sum[i] = (sum[i] % 100 + 100) % 100;
	}
	int maxx = 0;
	tong[0] = 1;
	for(int r = 1; r <= n; r++){
		for(int i = 0; i <= 99; i++){
			if(tong[i]){
				int tmp = sum[r] - i;
				tmp = (tmp % 100 + 100) % 100;
				maxx = max(maxx, tmp);
			}
		}
		tong[sum[r]] = 1;
	}
	cout << maxx;
	return 0;
}

U512645 数字选择2

这道题的步骤是:1.输出最大值;2.第一数是合法的数量;3.合法区间的最短长度,本身还是挺难的吧。
代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 2e5 + 5;
int maxx = 0, n;
int a[N], sum[N];
signed main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n;
	int minn = 0;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		sum[i] = sum[i - 1] + a[i];
		maxx = max(maxx, sum[i] - minn);
		minn = min(minn, sum[i]);
	}
	minn = sum[0];
	int cnt = 0, pos = 1, minn_len = 1e9;
	for(int i = 1; i <= n; i++){
		if(sum[i] - minn == maxx){
			cnt++;
			int l = pos + 1;
			int r = i;
			minn_len = min(minn_len, r - l + 1);
		}
		if(sum[i] < minn){
			minn = sum[i];
			pos = i;
		}
	}
	cout << maxx << endl << cnt <<' '<< minn_len;
	return 0;
}

好了,这次的总结就写到这了,感谢您的观看!😊😊😊

原文