From the perspective of poj2356, I want to know how the drawer works.

Source: Internet
Author: User
Find a multiple
Time limit:1000 ms Memory limit:65536 K
Total submissions:3089 Accepted:1370 Special Judge

Description

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 shoshould print to the output the single number 0. otherwise it shocould 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 shoshould print to the output only one (preferably your favorite) of them.

Sample Input

512341

Sample output

223

The question is to give a number n first, and then give n numbers. You need to select one or more numbers from the N numbers to make it a multiple of N.

If such an answer is not found, 0 is output.

There may be multiple answers, but Zhi Yong can output one solution at any time.

The first line of the output is the number of elements selected M, and then the value of the selected element is M.

At the beginning, I had a different idea about the drawer principle. After analysis, I realized that I had a better experience after last night.

In fact, this question must be resolved, and no result of 0 is output.

The proof is as follows:

We can find a [0], a [0] + A [1], A [0] + A [1] + A [2],..., A [0] + A [1] + A [2]... + A [n];

Assume sum [0], sum [1], sum [2],..., sum [N]

If one item exists as a multiple of N, the answer can be output directly from the first item.

But if it does not exist, the sum [I] % N value must be between [1, N-1], and because there are n sum, there is a drawer principle:

Put more than n objects in N drawers, then at least one drawer contains two or more objects.

Then there must be a pair of I, j, so that sum [I] = sum [J], where I! = J. Set j> I

Then (sum [J]-sum [I]) % n = 0, so sum [J]-sum [I] is a multiple of N

As long as the output is from I + 1 ~ All the values of a in J are the answer.

Then we can use this idea to directly solve the answer to the question.

At the beginning, it was because the code was written too long for the first time. In fact, the first and second cases can be considered as one case. For more information about the simplified code, see

Http://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj3370.html

Reference code:

1 # include <iostream>
2 # include <cstdlib>
3 # include <cstdio>
4 # include <cstring>
5 # include <algorithm>
6 # include <cmath>
7 using namespace STD;
8 int A [10000];
9 int mod [10000]; // mod storage determines whether sum % N has appeared. If-1 is not found, the value of K corresponding to sum is displayed, that is, the first K items and
10 int sum [10001]; // The sum storage is slightly different from the description, sum [k] = A [0] + A [1] +... + A [k-1];
11 int main ()
12 {
13 int N;
14 int I;
15 while (CIN> N)
16 {
17 memset (mod,-1, sizeof (MOD ));
18 sum [0] = 0;
19 For (I = 0; I <n; I ++)
20 {
21 CIN> A [I];
22}
23 For (I = 0; I <n; I ++)
24 {
25 sum [I + 1] = sum [I] + A [I];
26
27 if (sum [I + 1] % N = 0)
28 {// if it is a multiple of N, the output is
29 Int J;
30 cout <I + 1 <Endl;
31 For (j = 0; j <= I; j ++)
32 cout <A [J] <Endl;
33 break;
34}
35 if (mod [sum [I + 1] % N]! =-1)
36 {// if the remainder of the two numbers is found to be the same, the result is output in sequence
37 Int J;
38 cout <I-mod [sum [I + 1] % N] <Endl;
39 For (j = mod [sum [I + 1] % N] + 1; j <= I; j ++)
40 cout <A [J] <Endl;
41 break;
42}
43 mod [sum [I + 1] % N] = I; // store the remainder in mod. The value is
44}
45}
46 Return 0;
47}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.