1032. Find a multiple
Time Limit: 1.0 second
Memory Limit: 16 MB
The input contains N natural (i.e. positive integer) numbers (N ≤ 10000). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers (1 ≤ few ≤ N) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input
The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output
In
case your program decides that the target set of numbers can not be
found it should print to the output the single number 0. Otherwise it
should print the number of the chosen numbers in the first line
followed by the chosen numbers themselves (on a separate line each) in
arbitrary order.
If there are more than one set of numbers with required
properties you should print to the output only one (preferably your
favorite) of them.
Sample
input | output |
---|---|
5 1 2 3 4 1 |
2 2 3 |
Problem Source: Ural Collegiate Programming Contest '99
答案如下:
2
3 namespace Skyiv.Ben.Timus
4 {
5 // http://acm.timus.ru/problem.aspx?space=1&num=1032
6 /*
7 The input contains N natural (i.e. positive integer) numbers (N ≤ 10000).
8 Each of that numbers is not greater than 15000. This numbers are not necessarily
9 different (so it may happen that two or more of them will be equal). Your task is
10 to choose a few of given numbers (1 ≤ few ≤ N) so that the sum of chosen numbers
11 is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
12
13 As you know i can prove that there is 0 <= i < j <= n that:
14 ( a[i+1] +

15 because as box-principle (or pigeonhole) if we define
16 b[0] := 0 and b[j] := (a[1] +

17 we see b[x] es should be in range [0 .. n-1] but they are n+1 b (b[0] to b[n])
18 and it means there is two b (like (b[i], b[j]) that hey are equal: b[i] = b[j]
19 so we see:
20 s1 := a[1] +

21 and
22 s2 := a[1] +

23 so if we calculate s3 := s1 - s2 we see:
24 s3 := a[i+1] +

25 and we knew b[i] = b[j] so
26 s3= (p - q) * n ----> s3 mod n = 0
27 so we should calcualte all b[x] and find i and j
28 (with this algorithm we dont need to read all numbers!)
29 */
30 class T1032
31 {
32 static void Main()
33 {
34 int n = int.Parse(Console.ReadLine());
35 int[] a = new int[n];
36 int[] m = new int[n];
37 for (int i = 1; i < m.Length; i++) m[i] = -1;
38 for (int j = 0, b = 0; ; j++)
39 {
40 a[j] = int.Parse(Console.ReadLine());
41 b = (b + a[j]) % n;
42 if (m[b] == -1) m[b] = j + 1;
43 else
44 {
45 Console.WriteLine(j + 1 - m[b]);
46 for (int i = m[b]; i <= j; i++) Console.WriteLine(a[i]);
47 break;
48 }
49 }
50 }
51 }
52 }
上面程序使用的算法时间复杂度是 O(N),关键在于总可以在输入中的连续片段中找到符合要求的解,在程序前面的注释中已经证明了一点。
如果使用蛮力搜索尝试遍历每种可能的组合,则时间复杂度是 O(2N),不可能在题目要求的 1.0 秒之内完成。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步