题解:P12708 [KOI 2021 Round 1] 分割

· · 题解

题目大意

给定一个数组将其分成 4 段,每段和相同,每段数的数量必须大于 0 个,求总合法分割数量。

解题思路

如果给出的数组总和不能被 4 整除就不可行。

直接输出 0 。

否则就求总合法分割数量。

::::info

在 $s_i = s_n \div 4 , s_i = 2 (s_n \div 4) , s_i = 3(s_n \div 4)$ 时分割。 :::warning[注意] 要先判断第 $3$ 次分割,再判断第 $2$ 次分割,最后判断第 $1$ 次分割。 ::: :::: :::success[求数量部分] ```cpp for(int i = 1; i < n; i++){ if(s[i] == 3 * (s[n] / 4)) c3 += c2;//第3个分割点 if(s[i] == 2 * (s[n] / 4)) c2 += c1;//第2个分割点 if(s[i] == (s[n] / 4)) c1++;//第1个分割点 } ``` ::: ## AC :::success[附上完整代码] ```cpp #include<bits/stdc++.h> #define ll long long #define N 1000005 using namespace std; inline ll read(){//快读 ll x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-')f = -1; ch = getchar();} while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0'; ch = getchar();} return x * f; } ll n; ll s[N]; ll c1, c2, c3; int main() { n = read(); for(int i = 1; i <= n; i++) s[i] = s[i - 1] + read(); if(s[n] % 4 != 0){//总和不为4的倍数时无解 cout << 0; return 0; } for(int i = 1; i < n; i++){ if(s[i] == 3 * (s[n] / 4)) c3 += c2;//第3个分割点 if(s[i] == 2 * (s[n] / 4)) c2 += c1;//第2个分割点 if(s[i] == (s[n] / 4)) c1++;//第1个分割点 } cout << c3;//输出答案 return 0; } ``` :::