银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
Timus 1032. Find a multiple 要求在 N 个正整数中找出其中若干个使得它们的和是 N 的倍数。

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

inputoutput
5
1
2
3
4
1
2
2
3
Problem Author: Dmitry Filimonenkov
Problem Source: Ural Collegiate Programming Contest '99

答案如下:

 1 using System;
 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] +  + a[j] ) mod n = 0
15   because as box-principle (or pigeonhole) if we define
16     b[0] := 0 and  b[j] := (a[1] +  + a[j]) mod n
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] +  + a[j] = p * n + b[j]
21   and
22     s2 := a[1] +  + a[i] = q * n + b[i]
23   so if we calculate s3 := s1 - s2 we see:
24     s3 := a[i+1] +  + a[j] := (p - q) * n + b[j] - b[i]
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 秒之内完成。

posted on 2008-06-21 10:13  银河  阅读(1133)  评论(0编辑  收藏  举报