Poj2356 find a multiple (Drawer principle | Pigeon nest principle)

Source: Internet
Author: User
/* Reference question: N numbers are given, and if M numbers exist, the sum of M numbers is a multiple of n. If there are multiple groups of solutions, output a group at will. If it does not exist, the output value is 0. Question: First of all, we must declare that this question must be resolved. Principle Based on the drawer principle: Because there are n numbers, the remainder is obtained for N numbers. If there is no 0 in the remainder, according to the Pigeon nest principle, there must be two numbers with the same remainder. If the remainder is 0, naturally, it is a multiple of N. That is to say, the sum of N numbers must be multiples of N. The idea of this question is to obtain the sum of the remainder of the I (I <= N) and N from the first number, and record the existence status of the corresponding remainder in sequence, if sum = 0, the sum from the first item to the first item meets the meaning of the question. If the obtained sum already exists in the front, assume that the same sum value exists in the J (j <I, the sum of items J + 1 to I must satisfy the meaning of the question. */# Include <stdio. h> # include <string. h> # define Max 16000int s [Max], sum [Max], has [Max]; int main (void) {int N, I, j; while (scanf ("% d", & N )! = EOF) {int L, R; memset (has,-1, sizeof (has); memset (sum, 0, sizeof (SUM )); has [0] = 0; for (I = 1; I <= N; I ++) scanf ("% d", & S [I]); for (I = 1; I <= N; I ++) {sum [I] = (sum [I-1] + s [I]) % N; if (has [sum [I] =-1) has [sum [I] = I; else {L = has [sum [I]; R = I ;}} printf ("% d \ n", R-l); for (I = L + 1; I <= r; I ++) {printf ("% d \ n", s [I]) ;}} return 0 ;}

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.