【ACM】poj_2356_Find a multiple_201308061947

Find a multiple
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 4988  Accepted: 2159  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 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

5
1
2
3
4
1

Sample Output

2
2
3

Source

题目大意: 给出n个数,选出连续的若干m个数,使得和为n的倍数。输出m,以及任意顺序的m个数。
#include <stdio.h>
#define MAX 10100
int s1[MAX];
int s2[MAX];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,sum,begin,end;
        sum=begin=end=0;
        memset(s2,0,sizeof(s2));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&s1[i]);
        }
     
        for(i=1;i<=n;i++)
        {
            sum=(s1[i]+sum)%n;
            if(sum==0) {begin=1;end=i;break;}
            else if(!s2[sum])
            {s2[sum]=i;}
            else
            {
                begin=s2[sum]+1;
                end=i;
                break;
            }
        }
        //printf("%d ",begin);
        //printf("%d ",end);
        //if(i>=n)
        //printf("0 ");
        //else
        //{
        printf("%d ",end+1-begin);
        for(i=begin;i<=end;i++)
        printf("%d ",s1[i]);
        //}
    }
    return 0;   
}
//参考代码如下所示:
/*
#include<stdio.h>
#define MAX 10100
int a[MAX];
int locker[MAX];
int main()
{
 int n,i;
 while(scanf("%d",&n)!=EOF)
 {
  int sum=0,begin=0,end=0,num;
  for(i=1; i<=n; i++)
   scanf("%d",&a[i]);
  memset(locker,0,sizeof(locker)); 
  for(i=1; i<=n; i++)
  {
   sum=(a[i]+sum)%n;
   if(sum==0)
   {
    begin=1;
    end=i; break;
   }
   else if(!locker[sum])  //抽屉为空
   {
    locker[sum]=i;  //抽屉保存的是该元素在数组中的地址
   }
   else //抽屉不为空
   {
    begin=locker[sum]+1;
    end=i; break;
   }

  }
  num=end+1-begin;
  printf("%d ",num);
  for(i=begin; i<=end; i++)
   printf("%d ",a[i]);
 }
 return 0;
}
*/

组合数学之抽屉原理

•第一原理:
•1:把多于n个的物体放到n个抽屉里,则至少有一个抽屉里的东西不少于两件。
 
•2:把多于mn(m乘以n)个的物体放到n个抽屉里,则至少有一个抽屉里有不少于m+1的物体。
 
•3:把无穷多件物体放入n个抽屉,则至少有一个抽屉里 有无穷个物体。
 
原文地址:https://www.cnblogs.com/xl1027515989/p/3241866.html