AT_abc439_d [ABC439D] Kadomatsu Subsequence

思路

我们发现对于每一个 $j$,它同侧可以匹配的 $i,k$ 的顺序和位置并不影响贡献,所以考虑在线处理。
对于每一个数,当它可以被 $3$ 或 $7$ 整除时记录相应的商,因为 $a_i\le 1$,所以它不可能同时作为 $i,k$;当它可以被 $5$ 整除时,统计它前面所有的相应商,在答案中加上对应的 $3,7$ 的商的乘积即可。
从后到前也类似。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
#define fir first
#define sec second
#define int long long
#define pii pair<int,int>
#define fep(i,s,e) for(int i=s;i<e;i++)
#define pef(i,s,e) for(int i=s;i>e;i--)
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define per(i,s,e) for(int i=s;i>=e;i--)
namespace FastIO{
template<typename T>inline void read(T &x){
x=0;int f=1;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
}
template<typename T,typename...Args>
inline void read(T &x,Args&...args){
read(x);
read(args...);
}
template<typename T>void print(T x){
if(x<0)x=-x,putchar('-');
if(x>9)print(x/10);
putchar((x%10)^48);
}
}
using namespace std;
using namespace FastIO;
const int N=3e5+5;
int n,a[N],ans;
map<int,int>cnt3,cnt7;
signed main(){
read(n);
rep(i,1,n){
read(a[i]);
}
rep(i,1,n){
if(a[i]%3==0){
cnt3[a[i]/3]++;
}
if(a[i]%7==0){
cnt7[a[i]/7]++;
}
if(a[i]%5==0){
ans+=cnt3[a[i]/5]*cnt7[a[i]/5];
}
}
cnt3.clear();
cnt7.clear();
per(i,n,1){
if(a[i]%3==0){
cnt3[a[i]/3]++;
}
if(a[i]%7==0){
cnt7[a[i]/7]++;
}
if(a[i]%5==0){
ans+=cnt3[a[i]/5]*cnt7[a[i]/5];
}
}
print(ans);
}