【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\n",begin);
        //printf("%d\n",end);
        //if(i>=n)
        //printf("0\n");
        //else
        //{
        printf("%d\n",end+1-begin);
        for(i=begin;i<=end;i++)
        printf("%d\n",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\n",num);
  for(i=begin; i<=end; i++)
   printf("%d\n",a[i]);
 }
 return 0;
}
*/

 

组合数学之抽屉原理

 

•第一原理:
•1:把多于n个的物体放到n个抽屉里,则至少有一个抽屉里的东西不少于两件。
 
•2:把多于mn(m乘以n)个的物体放到n个抽屉里,则至少有一个抽屉里有不少于m+1的物体。
 
•3:把无穷多件物体放入n个抽屉,则至少有一个抽屉里 有无穷个物体。
 

转载于:https://www.cnblogs.com/xl1027515989/p/3241866.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值