银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
  268 随笔 :: 2 文章 :: 2616 评论 :: 140万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
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   银河  阅读(1142)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示