2020-2021 ICPC Southeastern European Regional E. Divisible by 3(前缀和,优化暴力)

Posted issue是fw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020-2021 ICPC Southeastern European Regional E. Divisible by 3(前缀和,优化暴力)相关的知识,希望对你有一定的参考价值。

LINK

区间 [ l , r ] [l,r] [l,r]的权值计算方式是( s u m = ∑ i = l r a i sum=\\sum\\limits_{i=l}^r a_i sum=i=lrai)

w e i g h t [ l , r ] = ∑ i = l r a i ∗ ( s u m − a i ) = s u m ∗ ∑ i = l r a i − ∑ i = 1 r a i 2 weight[l,r]=\\sum\\limits_{i=l}^r a_i*(sum-a_i)=sum*\\sum\\limits_{i=l}^r a_i-\\sum\\limits_{i=1}^r a_i^2 weight[l,r]=i=lrai(sumai)=sumi=lraii=1rai2

a i a_i ai的前缀和数组为 p 1 p_1 p1, a i 2 a_i^2 ai2的前缀和数组为 p 2 p_2 p2

w e i g h t [ l , r ] = ( p 1 [ r ] − p 1 [ l − 1 ] ) 2 − ( p 2 [ r ] − p 2 [ l − 1 ] ) weight[l,r]=(p_1[r]-p_1[l-1])^2-(p_2[r]-p_2[l-1]) weight[l,r]=(p1[r]p1[l1])2(p2[r]p2[l1])

(   p 1 [ r ] 2 + p 1 [ l − 1 ] 2 − 2 ∗ p 1 [ r ] ∗ p 1 [ l − 1 ]   ) − p 2 [ r ] + p 2 [ l − 1 ] = 3 ∗ f (\\ p_1[r]^2+p_1[l-1]^2-2*p_1[r]*p_1[l-1]\\ )-p_2[r]+p_2[l-1]=3*f ( p1[r]2+p1[l1]22p1[r]p1[l1] )p2[r]+p2[l1]=3f(其中 f f f为任意整数)

考虑枚举 r r r

3 ∗ f − p 1 [ r ] 2 + p 2 [ r ] = p 1 [ l − 1 ] ∗ ( p 1 [ l − 1 ] − 2 ∗ p 1 [ r ] ) + p 2 [ l − 1 ] 3*f-p_1[r]^2+p_2[r]=p_1[l-1]*(p_1[l-1]-2*p_1[r])+p_2[l-1] 3fp1[r]2+p2[r]=p1[l1](p1[l1]2p1[r])+p2[l1]

注意到运算实在模 3 3 3意义下,所以我们完全可以处理一个这样的数组 m [ i ] [ j ] [ k ] m[i][j][k] m[i][j][k]表示

p 1 [ r ] p_1[r] p1[r] 3 3 3 j j j的情况下,且 l − 1 ∈ [ 1 , i ] l-1\\in[1,i] l1[1,i]时,存在多少个下标满足

p 1 [ l − 1 ] ∗ ( p 1 [ l − 1 ] − 2 ∗ p 1 [ r ] ) + p 2 [ l − 1 ] = k p_1[l-1]*(p_1[l-1]-2*p_1[r])+p_2[l-1]=k p1[l1](p1[l1]2p1[r])+p2[l1]=k

这样一来,转移就是 O ( 1 ) O(1) O(1)的了,我们枚举 r r r,符合条件的 l − 1 l-1 l1索引的个数就是 m [ r − 1 ] [ j ] [ k ] m[r-1][j][k] m[r1][j][k]

其中 j = p 1 [ r ] % 3 j=p_1[r]\\%3 j=p1[r]%3 k = − p 1 [ r ] 2 + p 2 [ r ] k=-p_1[r]^2+p_2[r] k=p1[r]2+p2[r]

时间复杂度为 O ( n ) O(n) O(n2020-2021 ICPC Southeastern European M. Mistake(模拟,拓扑排序)

L. Let‘s Play Curling[思维]——2020-2021 ACM-ICPC, Asia Nanjing Regional Contest

2020-2021 ICPC Southeastern European Regional E. Divisible by 3(前缀和,优化暴力)

2020-2021 ACM-ICPC Latin American Regional Programming Contest(F. Fascinating Partitions)

2020-2021 ACM-ICPC Latin American Regional Programming Contest(F. Fascinating Partitions)

2020-2021 ACM-ICPC Brazil Subregional Programming Contest E题 Party Company树上倍增+树状数组维护