也可以从总方格数中扣除两张纸条覆盖的数量,再补回被重复扣除的 ab 个交叠方格,得到 nm−am−bn+ab。两种计算方式等价,均为满分方法。本题不要求学习一般的面积求并算法或容斥公式。
若整行整列的方格枚举实现正确,也允许通过公布的限制,不靠压缩时限排除这种基础做法。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, m, a, b;
cin >> n >> m >> a >> b;
cout << (n - a) * (m - b) << '\n';
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll c, w, x, t, r;
cin >> c >> w >> x >> t >> r;
w -= x;
if (w <= t) w += r;
if (w > c) w = c;
cout << w << '\n';
}
复杂度分析
时间复杂度为 O(1),空间复杂度为 O(1)。
C. 重开的节拍(beat)
题意概括
每小节有 k 拍,初始下一拍为第 1 拍。静音与敲击都会经过一拍,重新开始将下一拍改为第 1 拍但不经过一拍。统计重拍上的敲击次数。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
int p = 1, ans = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x == 2) {
p = 1;
} else {
if (x == 1 && p == 1) ans++;
p++;
if (p > k) p = 1;
}
}
cout << ans << '\n';
return 0;
}
复杂度分析
每条记录只处理一次,时间复杂度为 O(n),额外空间复杂度为 O(1)。
D. 双色试板(shade)
题意概括
第 i 张工作台的试板包含颜色 ai,api,aqi。所有请求都查询原始色卡。统计这三个颜色恰好有两种不同值的工作台数。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, l = 0, r, x;
int q;
cin >> n >> q;
r = n;
for (int i = 0; i < q; i++) {
cin >> x;
if (x % 2) l = max(l, x + 1);
else r = min(r, x - 1);
}
cout << max(0LL, r - l + 1) << '\n';
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n), d(n - 1);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i + 1 < n; i++) d[i] = a[i + 1] - a[i];
int l = 0;
while (l < n - 1) {
int r = l;
while (r + 1 < n - 1 && ((d[r] % 2 + 2) % 2) == ((d[r + 1] % 2 + 2) % 2)) r++;
sort(d.begin() + l, d.begin() + r + 1);
l = r + 1;
}
每个上界都有实际分配方案达到,所以答案就是这三个数的最大值。时间复杂度 O(nlogn),空间 O(n)。所有和使用 long long。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;vector<long long>a(n);long long S=0,L=0,H=0;for(auto &x:a){cin>>x;S+=x;}sort(a.begin(),a.end());
for(int i=0;i<n/3;++i){L+=a[i];H+=a[n-1-i];}cout<<max({S-3*L,3*H-S,H-L})<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long T,K;cin>>n>>T>>K;long long cur=1,ans=0;
for(int i=0;i<n;++i){long long d,b;cin>>d>>b;cur=max(cur,d);long long e=min(T,d+K-1);if(cur<=e){long long c=min(b,e-cur+1);ans+=c;cur+=c;}}
cout<<ans<<'\n';
}
C. 围村收费
算法与正确性
使用映射按车辆编号汇总记录。每辆车的进入时间初始为 00:00,离开时间初始为 24:00;出现 IN 时覆盖进入时间,出现 OUT 时覆盖离开时间。这同时处理了只有进入、只有离开和两条记录完整的情况,也不依赖输入顺序。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,x;cin>>n>>x;map<string,pair<int,int>>mp;
for(int i=0;i<n;++i){string id,t,op;cin>>id>>t>>op;int tm=stoi(t.substr(0,2))*60+stoi(t.substr(3,2));if(!mp.count(id))mp[id]={0,1440};if(op=="IN")mp[id].first=tm;else mp[id].second=tm;}
long long ans=0;for(auto p:mp){int t=p.second.second-p.second.first;int h=(max(0,t-120)+59)/60;ans+=min(24,h*x);}cout<<ans<<'\n';
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;
const ll inf = (1LL << 60);
int n;
ll a[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
ll sum = 0;
vector<ll> v[3];
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
v[a[i] % 3].push_back(a[i]);
}
for (int i = 0; i < 3; i++) {
sort(v[i].begin(), v[i].end());
}
int r = sum % 3;
if (r == 0) {
cout << n << '\n';
cout << sum << '\n';
return 0;
}
ll del = inf;
int cnt = 0;
if (r == 1) {
if ((int)v[1].size() >= 1) {
del = v[1][0];
若队首获胜,连续胜场加一;若挑战者获胜,他成为新队首,连续胜场从 1 开始。只要达到 k 就立即输出。
若扫描完所有选手仍未结束,当前队首已经是全局最大战斗力中最先出场者。他此后不可能失败,必然最终连续获胜 k 次,因此可以直接输出,不需要模拟可能高达 109 轮的比赛。
每名选手只在首次来到队首附近时处理一次,时间 O(n),空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long k;cin>>n>>k;vector<long long>a(n);for(auto &x:a)cin>>x;
int win=0;long long streak=0;for(int i=1;i<n;++i){if(a[win]>=a[i])++streak;else{win=i;streak=1;}if(streak>=k){cout<<win+1<<'\n';return 0;}}
cout<<win+1<<'\n';
}
C. 奇偶油箱
算法与正确性
设 dp[s][p] 表示处理完前若干个油箱、总油量恰好为 s、最后一个油箱油量奇偶性为 p 的方案数。
枚举当前油箱装入 x∈[0,ai] 升。除了第一个油箱外,只有 x 的奇偶性与前一个油箱不同才能转移:把原状态的方案数加到 ndp[s+x][xmod2]。同时要求 s+x≤m。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;while(T--){long long a[3],b[3];for(auto &x:a)cin>>x;for(auto &x:b)cin>>x;long long have=0,need=0;
for(int i=0;i<3;++i){long long d=a[i]-b[i];if(d>=0)have+=d/2;else need-=d;}cout<<(have>=need?"Yes":"No")<<'\n';}}
#include <bits/stdc++.h>
using namespace std;
bool ok(long long n,long long m,long long M){
long long s=0;
while(m&&s+1<M&&s<n){s+=s+1;--m;}
return s>=n||(__int128)s+(__int128)m*M>=n;
}
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;
while(T--){long long n,m;cin>>n>>m;if(!ok(n,m,n)){cout<<-1<<'\n';continue;}
long long l=1,r=n;while(l<r){long long mid=(l+r)/2;if(ok(n,m,mid))r=mid;else l=mid+1;}
cout<<l<<'\n';}
}
E. 因数加权和
算法与正确性
把“枚举每个数的所有因数”改成“枚举因数,再枚举它的倍数”。因数 d 对答案的贡献为
d+2d+⋯+⌊n/d⌋d=d2q(q+1),q=⌊n/d⌋.
于是答案为 ∑d=1ndq(q+1)/2。所有因数都被计入一次,因此这个换序与原式完全相同。
从左端点 l 出发,令 q=⌊n/l⌋。商仍为 q 的最右端点是 r=⌊n/q⌋,这一整段的贡献可以合并为
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);
long long n;cin>>n;__int128 ans=0;
for(long long l=1,r;l<=n;l=r+1){long long q=n/l;r=n/q;
ans+=(__int128)(l+r)*(r-l+1)/2*((__int128)q*(q+1)/2);}
cout<<(long long)ans<<'\n';
}
距离最近的整数只可能是 v 或 v+1。两者距离分别为 r/q 和 (q−r)/q。若 2r>q,选 v+1;否则选 v,其中等号情况恰好实现“距离相同选较小整数”。
C++ 的负整数除法向零取整,因此当 p % q 为负时,需要把商减一,并给余数加 q,才能得到数学上的向下取整。全部计算都可使用整数,避免浮点误差。
复杂度
时间 O(1),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){long long p,q;cin>>p>>q;if(q<0){p=-p;q=-q;}long long v=p/q,r=p%q;
if(r<0){--v;r+=q;}if(2*r>q)++v;cout<<v<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long x,y,a,b;cin>>n>>x>>y>>a>>b;long long ans=0;
for(int i=0;i<n;i++){long long t;cin>>t;if(t>x)ans+=a+((t+y-1)/y-1)*b;}cout<<ans<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);long long m,n,a,B=0;cin>>m>>n>>a;
for(int i=0;i<m;i++){long long b;cin>>b;B=max(B,b);}long long ans;
if(B>=a)ans=a+(n-1)*B;else{long long k=min(m,n);ans=k*a+(n-k)*B;}cout<<ans<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;long long mx[10];fill(mx,mx+10,-1);long long ans=-1;
for(int i=0;i<n;i++){long long v;cin>>v;long long t=v;int d=0;while(t){d=max(d,int(t%10));t/=10;}
if(mx[d]!=-1)ans=max(ans,mx[d]+v);mx[d]=max(mx[d],v);
}
cout<<ans<<'\n';
}
C. 回文
解题思路
统计每种字符出现次数 c。答案为
字符∑2c(c+1)。
先证明上界。每个长度大于 1 的回文子串,其左右端点一定是相同字符。一个子串由左右端点唯一确定,所以某种出现 c 次的字符,最多贡献 (2c) 个长度大于 1 的回文子串,再加上 c 个单字符回文。把各字符相加,就得到上述上界。
再证明可达。把相同字符全部放到一起,每个长度为 c 的同字符连续段,其所有 c(c+1)/2 个子串都是回文。各连续段已经达到上界,因此这种重排最优,无需真的构造新字符串。答案可能超过 32 位整数范围,应使用 long long。
复杂度
时间 O(n),额外空间 O(26)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);string s;cin>>s;long long cnt[26]={},ans=0;
for(char c:s)++cnt[c-'a'];for(long long c:cnt)ans+=c*(c+1)/2;cout<<ans<<'\n';
}
D. 选牌
解题思路
回文条件只约束成对位置 i 与 n+1−i,不同位置对之间互不影响。对每一对,直接枚举“上上、上下、下上、下下”四种选法,统计数值相等的选法数量 wi。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;vector<int>a(n),b(n);for(int &x:a)cin>>x;for(int &x:b)cin>>x;
long long ans=1;for(int i=0;i<n/2;i++){int j=n-1-i;int w=(a[i]==a[j])+(a[i]==b[j])+(b[i]==a[j])+(b[i]==b[j]);ans=ans*w%1000000;}
cout<<ans<<'\n';
}
最优方案可以取为:选择最大的 k+1 个原始数,用 k 次操作把它们合并成一个乘积,其余选中位置都变为 1。最终答案是
(i=1∏k+1ai)+k+i=k+2∑nai
(此处 a 已按从大到小排列)。最后再对 109+7 取模。
下面证明这种结构足够。把操作涉及的位置连边,一个含 s 个位置的连通块至少需要 s−1 次操作,且块内乘积不变。若该块原始乘积为 P,反复利用 u+v≤uv+1,块内最终和不超过 P+s−1,通过合并可以达到。
如果有两个非平凡连通块,乘积分别为 P,Q,从这两个块里取一个最小的原始数 v。将两个块合为一个、但把这个 v 留在块外,所需操作数不变,而乘积部分的总和由 P+Q 变为 PQ/v+v。由于 P,Q≥v,其增加量为 (P−v)(Q−v)/v≥0。反复合并后,只需保留一个非平凡块;不足 k 次的操作也可继续合并其他位置而不使和降低。
最后,若选中数 u 小于某个未选数 v,设其他选中数乘积为 R≥1,交换两者后的答案增加 (v−u)(R−1)≥0。所以选择最大的 k+1 个数一定最优。证明中比较的是实际整数大小,代码在确定选中数后才计算模意义下的乘积。
复杂度
时间 O(nlogn),额外空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,k;cin>>n>>k;vector<long long>a(n);for(auto &v:a)cin>>v;sort(a.rbegin(),a.rend());
const long long MOD=1000000007;long long p=1,ans=k;for(int i=0;i<=k;i++)p=p*a[i]%MOD;
ans=(ans+p)%MOD;for(int i=k+1;i<n;i++)ans=(ans+a[i])%MOD;cout<<ans<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
bool square(long long v){long long l=0,r=1000000000;
while(l<=r){long long mid=(l+r)/2,t=mid*mid;if(t==v)return true;if(t<v)l=mid+1;else r=mid-1;}return false;
}
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);const long long LIM=1000000000000000000LL;
vector<pair<int,long long>>v;long long f=1;
for(int x=1;;x++){if(f>(LIM-1)/x)break;f*=x;if(square(f+1))v.push_back({x,f+1});}
int T;cin>>T;while(T--){long long n;cin>>n;bool any=false;
for(auto p:v)if(p.second<=n){if(any)cout<<' ';cout<<p.first;any=true;}
if(!any)cout<<-1;cout<<'\n';
}
}
把金币数降序排列,设前 k 人金币总和为 Sk。若 Sk≥kx,让这 k 人一起平均分金币,就能使他们全部达到标准。
还需要说明,这个条件不能通过多次调整绕开。对任意 k,当前金币最多的 k 人的金币总和,在一次平均操作后不会增加:设最终选出的这 k 人中有 t 人参加了平均操作,他们得到的总和等于该平均集合总和的 t/∣集合∣,不大于平均前该集合中最富的 t 人的总和。再加上集合外选中的人,得到某个原先 k 人集合,其总和不超过原先最多的 k 人总和。
因此任何时刻若有 k 人每人不少于 x,原先前 k 人的金币总和必须不少于 kx。必要性和构造合在一起,答案就是满足 Sk≥kx 的最大 k。排序后累加检查,全部使用整数,无需浮点运算。
复杂度
每组时间 O(nlogn),额外空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;
while(T--){int n;long long x;cin>>n>>x;vector<long long>a(n);for(auto &v:a)cin>>v;sort(a.rbegin(),a.rend());
long long sum=0;int ans=0;for(int i=0;i<n;i++){sum+=a[i];if(sum>=1LL*(i+1)*x)ans=i+1;}cout<<ans<<'\n';
}
}
E. 怪兽胜数
解题思路
从右向左扫描。处理位置 i 时,数据结构中保存的正好是 i 右侧所有怪兽的编号。查询其中严格小于 Ai 的数量,便得到 Bi,然后将 Ai 加入结构。
因此先计算 5c−20(b−d),若 5c>M 再加 20d,最后与 0 取最大值。判断条件是“严格超过”,不能把等于 M 的情况算作基础达标;是否达标也应根据基础题得分判断,不能用最终总分判断。
复杂度
时间 O(1),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){int a,b,c,d,M;cin>>a>>b>>c>>d>>M;int ans=5*c-20*(b-d);if(5*c>M)ans+=20*d;cout<<max(0,ans)<<'\n';}
C. 训练营补给准备
解题思路
学员只吃中午一餐,一天共需要 b 份学员餐;教练中午和晚上都吃,一天共需要 2a 份教练餐。
主食的总需求为 bR+2a⋅2R=(b+4a)R;蔬菜和肉类的总需求分别为 (b+6a)V、(b+6a)M。这些式子逐类、逐餐累计了每个人的需求,没有遗漏或重复。计算乘法时使用 long long。
复杂度
时间 O(1),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){long long a,b,R,V,M;cin>>a>>b>>R>>V>>M;cout<<(b+4*a)*R<<' '<<(b+6*a)*V<<' '<<(b+6*a)*M<<'\n';}
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin>>n;long long v=0;
while(n--){long long x;cin>>x;v^=x;}cout<<v<<" 0\n";
}
按下标从小到大扫描数组,维护已经遇到多少个 x。每遇到一个 x,计数加一;计数第一次达到 k 时,当前下标就是所求位置。扫描结束仍未达到 k,则输出 Error。
扫描顺序就是下标递增顺序,所以计数为 k 时恰好对应 x 的第 k 次出现。注意下标从 1 开始,且错误提示大小写必须与题目一致。
复杂度
时间 O(n),额外空间 O(n)。因为 k,x 在数组之后输入,先保存数组。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);
int n;cin>>n;vector<int>a(n+1);for(int i=1;i<=n;i++)cin>>a[i];
int k,x,cnt=0;cin>>k>>x;
for(int i=1;i<=n;i++)if(a[i]==x&&++cnt==k){cout<<i<<'\n';return 0;}
cout<<"Error\n";
}
B. 质数终点
解题思路
如果 n 本来就是质数,答案为 0。否则,取 n 的任意一个质因数 p,选择除数 x=n/p,一次操作就能把 n 变成 p。因此合数的答案一定为 1,无需真的构造操作。
判断 n 是否为质数时,枚举 2≤d≤n,只要找到一个整除 n 的 d 就说明它是合数。若不存在这样的因数,则 n 为质数,因为合数的一对非平凡因数中至少有一个不超过平方根。
复杂度
时间 O(n),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);long long n;cin>>n;
for(long long d=2;d<=n/d;d++)if(n%d==0){cout<<1<<'\n';return 0;}
cout<<0<<'\n';
}
C. 城市出行
解题思路
车票只能当天使用,各天的购买决策互不影响。第 i 天要么购买 ai 张单程票,花费 aix;要么购买一张单日票,花费 y。混合买票不会比其中较便宜的一种更优。
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n;long long x,y;cin>>n>>x>>y;
long long ans=0;for(int i=0;i<n;i++){long long a;cin>>a;ans+=min(a*x,y);}cout<<ans<<'\n';
}
每个门都选择最早允许的通过时刻。若更早到达下一个门,始终可以等待到任意较晚到达方案的通过时刻,所以更早到达不会使后续行程变差。由此逐门贪心即可得到全程最早到达时刻。时刻可能累积到很大,必须使用 long long。
复杂度
每组时间 O(n),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin>>T;
while(T--){int n;cin>>n;long long t=0;
for(int i=0;i<n;i++){long long a,b;cin>>a>>b;long long r=t%(a+b);if(r>=a)t+=a+b-r;++t;}
cout<<t<<'\n';
}
}
也就是说,两个位置能配对,当且仅当它们的 ai−i 相等。顺序扫描数组,用哈希表记录每个值已经出现了多少次。处理第 i 个位置时,之前相同值的出现次数就是能和它配对的数量,然后再把当前位置加入计数。每个位置只处理一次,能通过全部数据。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
unordered_map<ll, ll> mp;
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll x;
cin >> x;
ll v = x - i;
ans += mp[v];
mp[v]++;
}
cout << ans << '\n';
}
return 0;
}
复杂度分析
最终算法对每个元素进行一次哈希表查询和更新,总时间复杂度为 O(∑n),空间复杂度为 O(∑n)。
B. 模段(mseg)
题意概括
给定多组序列和模数 m,统计有多少个非空连续区间的元素和能被 m 整除。所有测试的长度总和不超过 2×105。
算法3:
设 si 为前 i 个数的和对 m 取模后的结果,区间 [l,r] 的和能被 m 整除,当且仅当 sr=sl−1。于是问题转化为统计相同前缀余数的二元组。
顺序扫描数组,初始时余数 0 出现一次。每得到一个新的前缀余数,就把之前相同余数的出现次数加入答案,再把当前余数记入哈希表。因为 m 可以很大,仍然使用哈希表而不是长度为 m 的数组。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
ll m;
cin >> n >> m;
unordered_map<ll, ll> mp;
mp[0] = 1;
ll s = 0, ans = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
s = (s + x) % m;
ans += mp[s];
mp[s]++;
}
cout << ans << '\n';
}
return 0;
}
算法2:
当 k=2 时,只需要统计奇偶性,相同奇偶的两个数相加才是偶数。特殊性质 B 中所有数已经小于 k,可以直接按数值当作余数计数。这两类数据都指向同余分类。时间复杂度为 O(n),期望得分:30分。
算法3:
对一个数 x,只关心它的余数 r=xmodk。为了让两数之和能被 k 整除,另一个数的余数必须是 (k−r)modk。顺序扫描数组,处理当前数时先查询之前已经出现的互补余数个数,把它加入答案,再记录当前余数。
由于 k 可能很大,不能开长度为 k 的数组,使用哈希表保存实际出现过的余数即可。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
ll k;
cin >> n >> k;
unordered_map<ll, ll> mp;
ll ans = 0;
for (int i = 0; i < n; i++) {
ll