zoukankan      html  css  js  c++  java
  • poj 2356 Find a multiple(鸽巢原理)

    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个数,求是否存在一些数的和是n的倍数。若存在,输出即可。不存在,输出0.

    思路:鸽巢原理的题目,组合数学课本上的原题。可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 using namespace std;
    15 #define max(a,b) (a) > (b) ? (a) : (b)  
    16 #define min(a,b) (a) < (b) ? (a) : (b)
    17 #define ll long long
    18 #define eps 1e-10
    19 #define MOD 1000000007
    20 #define N 10006
    21 #define inf 1e12
    22 int n;
    23 int sum[N];
    24 int vis[N];
    25 int a[N];
    26 int tmp[N];
    27 int main()
    28 {
    29     while(scanf("%d",&n)==1){
    30         memset(sum,0,sizeof(sum));
    31         for(int i=1;i<=n;i++){
    32             //int x;
    33             scanf("%d",&a[i]);
    34             sum[i]=sum[i-1]+a[i];
    35         }
    36         memset(vis,0,sizeof(vis));
    37         memset(tmp,0,sizeof(tmp));
    38         for(int i=1;i<=n;i++){
    39             int x=sum[i]%n;
    40             if(vis[x]){
    41                 int y=tmp[x];
    42                 printf("%d
    ",i-y);
    43                 for(int j=y+1;j<=i;j++){
    44                     printf("%d
    ",a[j]);
    45                 }
    46                 break;
    47                 
    48             }
    49             if(x==0){
    50                 printf("%d
    ",i);
    51                 for(int j=1;j<=i;j++){
    52                     printf("%d
    ",a[j]);
    53                 }
    54                 break;
    55             }
    56             vis[x]=1;
    57             tmp[x]=i;
    58         }
    59         
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    插入排序
    选择排序
    二分法查找
    pickle模块的使用
    循环和递归求阶乘、递归实现汉诺塔程序、递归实现拆出每个位的数字放在列表中、递归求第五个人的年龄
    app测试和测试基础
    TCP协议中的三次握手和四次挥手及其他
    AcWing 122 糖果传递
    AcWing 121 赶牛入圈 (二分+离散化)
    AcWing 120 防线 (二分)
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4811449.html
Copyright © 2011-2022 走看看