题解:P10740 [SEERC2020] Divisible by 3
xiezheyuan · · 题解
思路
记
发现
将无序二元组,转成有序二元组,并且允许
令
记
发现决定
我们倒序枚举
时间复杂度
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int a[N], n, f[N], g[N], cnt[5][5], ans;
int M(int x) {return (x % 3 + 3) % 3; }
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];
a[i] = a[i] % 3;
f[i] = (f[i - 1] + a[i]) % 3;
g[i] = (g[i - 1] + a[i] * a[i]) % 3;
}
for(int l=n;l;l--){
cnt[f[l]][g[l]]++;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(M((i - f[l - 1]) * (i - f[l - 1]) - j + g[l - 1]) == 0) ans += cnt[i][j];
}
}
}
cout << ans << '\n';
return 0;
}
// Written by xiezheyuan