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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述