zoukankan      html  css  js  c++  java
  • POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7192   Accepted: 3138   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$个自然数,试找出其中的若干个数,它们的算术和是$N$的整数倍。

    基本思路

      1、若存在$sum\%N==0$,则$sum$一定是$N$的倍数(即$sum==kN, kin [1, infty]$)。

      2、若$sum[i]equiv sum[j] mod N$,则$(sum[j]-sum[i])\%N==0$,即$sum[j]-sum[i]$是$N$的倍数。

      3、(鸽巢原理)将$N$个物体放入$N-1$个盒子里,则一定至少有$1$个盒子放了$2$个以上的物体。这里我们将$N$个自然数分别记为$arr[i], iin [1, N]$,令$sum[i]=(sumlimits^{N}_{i=1}arr[i])\%N, iin [1, N]$,而$sum[i]$的取值范围为0 ~ N-1,因此:A.必然存在$i, jin [1, N]$使得sum[i]==sum[j];B.可以存在$iin [1, N]$有sum[i]==0。

      4、建立一个标记数组$sgn[]$,标记$sum[]$的值,这样可以在读入预处理时找到相等的两个$sum$。同时根据第3点,我们得知答案必然存在,不会出现没有答案输出0的情况。

      5、由于存在sum[i]==0的情况,所以要初始化sgn[0]=0。

    代码

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int N, arr[10010], sum[10010], sgn[10010];
     5 
     6 int main() {
     7     int l=0, r=-1;
     8     memset(sgn, 0xFF, sizeof(sgn)); sgn[0]=0;
     9     scanf("%d", &N);
    10     for(int i=1; i<=N; i++) {
    11         scanf("%d", arr+i);
    12         sum[i]=(sum[i-1]+arr[i])%N;
    13         if(!~sgn[sum[i]])
    14             sgn[sum[i]]=i;
    15         else {
    16             l=sgn[sum[i]];
    17             r=i;
    18         }
    19     }
    20     printf("%d
    ", r-l);
    21     for(int i=l+1; i<=r; i++)
    22         printf("%d
    ", arr[i]);
    23     return 0;
    24 }
    POJ 2356

    另外,POJ3370也是与此题一样的做法,此题题解在这里

    ——本文原创by BlackStorm,转载请注明出处。

    本文地址:http://www.cnblogs.com/BlackStorm/p/5243156.html

  • 相关阅读:
    编写 grunt 插件经验
    Sencha Touch 手机移动开发框架 HTML5 项目压缩方案;
    随笔 编辑推荐 上头条了, 贴出来做个记念!
    Javascript 俄罗斯方块 游戏代码解释!
    30天自制操作系统(NASM+GCC版)
    Logisim 打不开的解决方案(Windows10)
    Kali Linux 2020通过UEFI硬盘安装(免u盘)
    开源一个自制的ORM框架,基于Java原生JDBC(应该是全网首个吧)
    书单
    前端技术文章收集
  • 原文地址:https://www.cnblogs.com/BlackStorm/p/5243156.html
Copyright © 2011-2022 走看看