Find a multiple POJ

来源:互联网 发布:linux 脚本 批量执行 编辑:程序博客网 时间:2024/05/24 01:49


 Find a multiple POJ - 2356 

       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). 

       The first line of the input contains the single number N. Each of next N lines contains one number from the given set.

        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.

Input

5
1
2
3
4
1

Output

2

2

3


       题目比较水吧,Mark 一种思想,鸽笼原理。
       题意:  给你 n 个数,让你找出 k  个数满足  k个数的累加和 是 n 的倍数。
       
       思路: 鸽笼原理:  n+1 个鸽子放在 n  个笼子里面,肯定有一个笼子里面有 2 只;
       对于这个题我们可以考虑,前 n 个数的前缀和如果不存在模 n 等于 0 的情况,一定存在 余数 相等的情况,那么中间部分就满足条件。  (因为只有n-1 种余数)
      对于等于0的情况直接 就是前 i 个满足情况。
      输出好像就是输出数字,而不是下标。


#pragma comment(linker, "/STACK:1024000000,1024000000")//#include <bits/stdc++.h>#include<string>#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<stack>#include<vector>#include<set>#include<algorithm>#define maxn 10600#define INF 0x3f3f3f3f#define eps 1e-8#define MOD 1000000007#define ll long longusing namespace std;int A[maxn],vis[maxn],sum[maxn];int main(){    int n;    while(scanf("%d",&n)!=EOF){    memset(vis,-1,sizeof vis);    sum[0]=0;    int a,b;    vis[0]=0;    for(int i=1;i<=n;i++)        scanf("%d",&A[i]);    for(int i=1;i<=n;i++)    {        sum[i]=(sum[i-1]+A[i])%n;        if(vis[sum[i]]==-1)            vis[sum[i]]=i;        else        {            a=vis[sum[i]];            b=i;            break;        }    }    printf("%d\n",b-a);    for(int j=a+1;j<=b;j++)        printf("%d\n",A[j]);    }    return 0;}





 

原创粉丝点击