跳到正文

个人记录

7.31/8.1课堂自我总结

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

文章信息

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

版本

  1. v1当前

1.课堂笔记

模法公式

加法

(a+b)%c=(a%c+b%c)%c;

减法

(a-b)%c=(a%c-b%c)%c;

乘法

(a*b)%c=(a%c * b%c)%c;

负变正

a%c=(a%c+c)%c;

memset

memset(数组名,赋值X,sizeof(数组名));

数组内的所有字节都变成X;

sizeof(获取字节);

同余前缀和

sum[r]%P==sum[l-1]%P;

1<=r>=n;

0<=l>=n-1;

注意:循环的开始一定是0;

2. 题目总结

U408366 环数

思路:

因为本题中的数据较大,枚举肯定是不行的,所以我们这里采用前缀和进行优化。具体看代码注解。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+5;
ll a[N];
ll s[N];
ll n,m;
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n>>m;
		ll maxn=-1e18;
		ll maxl=0;
		
		for(int i=1;i<=n;i++){
			cin>>a[i];
			a[n+i]=a[i];//断环成链,将数组复制一份;
			
		}
		for(int i=1;i<=2*n;i++){
			s[i]=s[i-1]+a[i];//前缀和
		}
		for(int i=1;i+m-1<=2*n;i++){//重点!不是i<=2*n是i+m-1<=2*n这样就实现了搜索一个环;
			int l=i;
			int r=i+m-1;//向后查询m个数
			if(maxn<s[r]-s[l-1]){
				maxn=s[r]-s[l-1];
				maxl=i;
			}
		}
		cout<<maxn<<" "<<maxl<<endl;
	}
   	return 0;
}

CF313B Ilya and Queries

思路

这题就是一个普通的前缀和,只是因为前缀和的特性,字符串的下标需要从1开始,所以我们需要在字符串s的前面+上和题目中字符不同的字符;

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+5;
int a[N]; 
int main(){
	string s;
	cin>>s;
	int len=s.size();
	s="@"+s+"@";
	for(int i=1;i<=len;i++){
		a[i]=a[i-1]+(s[i]==s[i+1]);//如果一样前缀和+1;
	}
	int m;
	cin>>m;
	while(m--){
		int l,r;
		cin>>l>>r;
		r--;//防止重复;
		cout<<a[r]-a[l-1]<<endl;
	}
   	return 0;
}

U427199 书柜

思路

在本题中我们把高的数标记成1,矮的标记成0,这样就只要用前缀和记录有多少个“01”就可以了;

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+5;
int t,n;
int ha,hb;
int a[N];
int sum[N];
string s;
int main(){
	cin>>n>>t;
	cin>>ha>>hb;
	if(ha==hb){
		while(t--){
			cout<<1<<endl;
		}
	}else{
		cin>>s;
		s=" "+s+" ";
		for(int i=1;i<=n;i++){
			if(s[i]=='A'){
				a[i]=(ha>hb);
			}else{
				a[i]=(hb>ha);
			}
		}
		for(int i=1;i<=n;i++){
			sum[i]=sum[i-1]+(a[i]==0&&a[i+1]==1);
		}
		while(t--){
			int l,r;
			cin>>l>>r;
			r--;
			if(sum[r]-sum[l-1]==0){
				cout<<1<<endl;
			}else{
				cout<<2<<endl;
			}
        }
	}
   	return 0;
}

P3131 [USACO16JAN] Subsequences Summing to Sevens S

思路

本题数据量较大,所以普通的前缀和+枚举就会TLE,所以我们就要用到同余前缀和。定两个数组pre,suf.pre记录开头的位置,suf记录结尾的位置;最后打擂台找出最长的子段;

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
ll n;
ll a[N];
ll sum[N];
ll pre[10];
ll suf[10];
string s;
int main(){
	cin>>n;
	memset(pre,-1,sizeof(pre));
	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++){
   		sum[i]%=7;
   		if(pre[sum[i]]==-1){
   			pre[sum[i]]=i;
		}
		suf[sum[i]]=i;
	}
	for(int i=0;i<7;i++){
		int l=pre[i]+1;
		int r=suf[i];
		maxx=max(maxx,r-l+1);
	}
	cout<<maxx; 
	return 0;
   	
}

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

思路

本题同上题也会用到同余前缀和,定义一个数组t,记录sum%k的结果,然后利用高斯定理计算出每个t的数量就可以了;

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
ll n,k;
ll a[N];
ll sum[N];
ll t[N];
ll maxx;
string s;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i]; 
	}
   	for(int i=0;i<=n;i++){
   		t[sum[i]%k]++;
	}
	for(int i=0;i<=k-1;i++){
		maxx=maxx+t[i]*(t[i]-1)/2;
	}
	cout<<maxx;
	return 0;
   	
}

U512656 数字选择3

思路

本题还是同余前缀和有玩没玩了,做法大概和前面的题一样,只是要注意在这里sum[i]可能是个负数,所以取余方法是(x%100+100)%100;

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e6+5;
int n;
int a[N];
int sum[N];
int t[N];
int get(int x){
	return (x%100+100)%100;
}
int main(){
	int ans=0;
	t[0]=1;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
		sum[i]=get(sum[i]);
		for(int j=0;j<=99;j++){
			if(t[j])
			ans=max(ans,get(sum[i]-j));
		}
		t[sum[i]]=1;
	}
	cout<<ans;
	return 0;
   	
}

完结✿✿ヽ(°▽°)ノ✿

原文