poj 2356 抽屉问题

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <assert.h>
  4. #include <string.h>
  5. #define MAX 10001
  6. //[(物体数-1)÷抽屉数]+1
  7. //给出N 个数,在其中找出m个数,使得m个数的和是N的倍数,输出m以及任意顺序这m个数,只要输出一种情况。
  8. /*
  9. 可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,
  10. 根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。
  11. 也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。
  12. */
  13. int num[MAX],locker[MAX];
  14. int main()
  15. {
  16. int n;
  17. while(~scanf("%d", &n) ) //number : n;
  18. {
  19. for(int i=1; i<=n; i++) //input number
  20. scanf("%d", &num[i] );
  21. int sum=0;
  22. memset(locker, 0, sizeof(locker) ); //set 0 of 抽屉
  23. int begin=0, end=0; //set start and end
  24. for(int i=1; i<=n; i++)
  25. {
  26. sum = (sum+num[i]) % n; //前n个数求余
  27. if(sum == 0) //是0就是从头开始到尾
  28. {
  29. begin=1;
  30. end=i;
  31. break;
  32. }
  33. else if( !locker[sum]) //不是件以余数为下标保存在抽屉中,抽屉内为该元素在在数组中地址
  34. {
  35. locker[sum]=i;
  36. }
  37. else //是,即sum[i]==sum[j],取i+1到j个数,所以begin要加一
  38. {
  39. begin=locker[sum]+1;
  40. end=i;
  41. break;
  42. }
  43. }
  44. printf("%d\n", end-begin+1); //多少个数,注意加一
  45. for(int i=begin; i<=end; i++) //输出
  46. printf("%d\n", num[i] );
  47. }
  48. return 0;
  49. }
  50. /*
  51. Description
  52. 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).
  53. Input
  54. The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
  55. Output
  56. 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.
  57. 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.
  58. Sample Input
  59. 5
  60. 1
  61. 2
  62. 3
  63. 4
  64. 1
  65. Sample Output
  66. 2
  67. 2
  68. 3
  69. */

来自为知笔记(Wiz)

附件列表

时间: 2024-08-09 07:55:27

poj 2356 抽屉问题的相关文章

POJ 2356 (抽屉原理)

题目中说:随便输出一组.抽屉原理可以求出取出的数连续的情况. #include <iostream> #include<cstdio> using namespace std; int a[10005],s[10005]; int n,start,end,ok = 0; int main() { while(scanf("%d",&n) != EOF ) { for(int i = 1; i <= n; i++) { scanf("%d&

POJ 2356 Find a multiple 抽屉原理

从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首先利用前缀和先预处理一下,然后如果sum[i]==0的话,很显然就直接输出i,然后接下来从第一位一直输出到第i位就行了 然后接下来直接用一个mod数组表示上一个答案为这个mod的时候的编号是多少 就是mod[sum[i]%n]=i; 然后判断一下if(mod[sum[i]%n]!=0)然后就直接从m

poj 2356 Find a multiple (剩余类,抽屉原理)

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6965   Accepted: 3052   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000

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

POJ 2356 Find a multiple (dp + 鸽笼原理)

OJ题目:click here~~ 题目分析:n个数,从中取若干个数,和为n的倍数.给出一种取法. 因为只要给出其中一种方案就行,鸽笼原理可以求出取出的数为连续的方案. 关于鸽笼原理,点这里~ 直接贴过来: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两个以上物品. 如果你知道这个结论: a1,a2,a3...am是正整数序列,至少存在整数k和r,1<=k<r<=m,使得ak+a(k+1)+...+a(r)是m的倍数. 证明比较简单: Sk表示前k个数之和

poj 2356 Find a multiple 鸽巢原理的简单应用

题目要求任选几个自然数,使得他们的和是n的倍数. 由鸽巢原理如果我们只选连续的数,一定能得到解. 首先预处理前缀和模n下的sum,如果发现sum[i]==sum[j] 那么(sum[j]-sum[i])%n一定为0,直接输出i+1~j就够了. 为什么一定会有解,因为sum从1~n有n个数,而模n下的数只有0~n-1,把n个数放入0~n-1个数里,怎么也会有重复,所以这种构造方法一定没问题. 其实可以O(n)实现,嫌麻烦,就二重循环无脑了. #include <iostream> #includ

POJ 2356 Find a multiple 鸽巢原理

题目来源:POJ 2356 Find a multiple 题意:n个数 选出任意个数 使得这些数的和是n的倍数 思路:肯定有解 并且解是连续的一段数 证明: 假设有m个数 a1,a2,a3...am    s1 s2 s3...sm为前缀和 s1 = a1 s2 = a1+a2 s3 = a1+a2+a3... sm = a1+a2+a3+...+am 1.如果某个前缀和si%m == 0 那么得到解 2.设x1=s1%m x2 = s2%m x3 = s3%m xm = sm%m 因为1不成

POJ 2356 find multiple 鸽巢原理

我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>jquery test</title> <script src="jquery-1.11.1.min.js">&

POJ 2356 &amp;&amp; POJ 3370 鸽巢原理

POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数的和对n的取模 sum[0] = 0; 那么sum[0] ~ sum[n]有n+1个数据,这些数据的范围都是 0~n , 要是存在 sum[i] = 0,那么输出前 i 个数据即可 要是不存在那根据鸽巢原理可以说明必然能找到一个 sum[i] = sum[j]  ,那么说明 (sum[i+1] +