题面
An equal sum partition of a sequence of numbers is a grouping of the numbers (in the same order as the original sequence) in such a way that each group has the same sum. For example, the sequence:
2 5 1 3 3 7
may be grouped as:
(2 5) (1 3 3) (7)
to yield an equal sum of 7.
Note: The partition that puts all the numbers in a single group is an equal sum partition with the sum equal to the sum of all the numbers in the sequence.
For this problem, you will write a program that takes as input a sequence of positive integers and returns the smallest sum for an equal sum partition of the sequence.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. The first line of each data set contains the data set number, followed by a space, followed by a decimal integer M, (1 ≤ M ≤ 10000), giving the total number of integers in the sequence. The remaining line(s) in the dataset consist of the values, 10 per line, separated by a single space. The last line in the dataset may contain less than 10 values.
Output
For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the smallest sum for an equal sum partition of the sequence.
Sample Input
3
1 6
2 5 1 3 3 7
2 6
1 2 3 4 5 6
3 20
1 1 2 1 1 2 1 1 2 1
1 2 1 1 2 1 1 2 1 1
Sample Output
1 7
2 21
3 2
题意
给一串数字,问能否把这些数字分成连续的几个区间,并且每个区间里数字的和相等,输出最小的区间和
思路
可以暴力先算出所有数字的和sum,再枚举1-sum,找出可以整除的第一个值(说明可以将这个值平均分成几份)
代码
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int s[202020];
int main()
{
int t;
cin>>t;
while(t--)
{
int a, n,sum=0,res;
cin>>a>>n;
for (int i = 1; i <= n;i++)
{
cin >> s[i];
sum += s[i];//求出所有数字的和
}
int flag,b;
for (int i = 1; i <= sum;i++)
{
if(sum%i==0)//说明sum能将i平均分成几份,使每一份的和都是i
{
flag = 1;
b = 0;
for (int j =1; j <= n;j++)
{
b += s[j];
if(b==i)
b = 0;//求出每一个等于i的区间 将b清零
if(b>i)
{
flag = 0;//说明不能等于i跳出
break;
}
}
}
if(flag)
{
res = i;//当flag-1的时候说明i就是要算的第一个符合题意的值
break;
}
}
cout << a << " " << res << endl;
}
}