版本比较
8.6日CSP-J 2025年真题考试
正文
增加 159 行删除 9 行
127 行未改动全部展开128129130131132132134133134136135136138137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194142195196197198199200146147201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247152248249250251252253254255256257258259260261262263264265159266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310127 行未改动全部展开有一个长度为$n$的序列$a$,为你最多有几个不相交区间的异或和等于$k$
### 思路
**40分思路**
直接三重循环暴力模拟即可**60分思路**
**40分代码**三重循环算出每个区间异或和,把异或和等于$k$的全部存入区间列表,再将区间按右端点从小到大排序,用贪心选区间:每次选当前左端点大于上一个选中区间右端点的区间,更新`last`为该区间右端点,计数加$1$即可
```cpp**60分代码**
```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;const int N=2e6+5;ll n,k;ll a[N];struct P{ ll l,r;};vector<P> q;ll t[N];ll cmp(P a,P b){ return a.r<b.r;}signed main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); t[0]=0; cin>>n>>k; for(int i=1;i<=n;i++) { cin>>a[i]; } for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { ll sum=0; for(int x=i;x<=j;x++) { sum^=a[x]; } if(sum==k) { q.push_back({i,j}); } } } sort(q.begin(),q.end(),cmp); ll last=-1,res=0; for(auto e : q) { if(e.l>last) { last=e.r; res++; } } cout<<res; return 0;}```
**60分思路****100分思路**
利用前缀异或性质,区间$[l,r]$异或和为k等价于$sum[r]\oplus sum[l-1]=k$,用数组t记录前缀异或值上一次出现的下标,遍历右端点j,若`sum[j]^k`曾经出现过就得到合法区间存入队列,生成的区间右端点递增,直接贪心选取不重叠区间,每次选左端点大于上一个选中区间右端的区间,统计最多数量
### 代码
**60分代码**
```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;struct P{ ll l,r;};const int N=2e6+5;ll n,k;ll a[N],sum[N],t[N];vector<P> q;signed main(){ ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); memset(t,-1,sizeof t); t[0]=0; cin>>n>>k; for(int i=1;i<=n;i++) { cin>>a[i]; sum[i]=sum[i-1]^a[i]; } for(int j=1;j<=n;j++) { ll x=sum[j]^k; if(t[x]!=-1) { q.push_back({t[x]+1,j}); } t[sum[j]]=j; } ll last=-1,res=0; for(auto e : q) { if(e.l>last) { last=e.r; res++; } } cout<<res; return 0;}
```
**100分思路**### 个人总结
不是很难,并且还讲过,结果没有写出来😅
## T4题目解析
### 题意
有$n$木棍,要你拼成一个满足根数大于$3$,木棍长度和大于最长木棍的$2$倍,问你有几种方案满足条件
### 思路
把数组排序,枚举每根木棍作为选出集合的最大值,背包统计前面木棍选若干根的方案,满足选出总和大于当前木棍长度,累加合法方案
### 代码
```cpp
#include<bits/stdc++.h>#define ll long longusing namespace std;ll n ;const int N=5e3+5;const int mod=998244353;ll a[N];ll res;ll dp[2][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]; } sort(a+1,a+n+1); dp[0][0]=1; ll super=0; for(int i=1;i<=n;i++) { for(int j=a[i]+1;j<=5000;j++) res=(res+dp[0][j])%mod; res=(res+super)%mod; super=(super*2)%mod; for(int j=5001-a[i];j<=5000;j++) super=(super+dp[0][j])%mod; for(int j=0;j<=5000;j++) { if(j-a[i]>=0) dp[1][j]=(dp[0][j]+dp[0][j-a[i]])%mod; else dp[1][j]=dp[0][j]; } for(int j=0;j<=5000;j++) dp[0][j]=dp[1][j]; } cout<<res; return 0;}```
### 个人总结
这道题我写了$40$分的爆搜,但是老师讲过正解,但我没写除了,忘了
## 考试总结
虽然这次我$260$分,过一等分数线了,但是我不满意,因为所有题老师都讲过,至少都要$300$分