B - 抽屉 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).

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

题意:

输入n

输入n个数

判断这n个数中是不是有几个数字之和是n的倍数

思路:

n个数余数分别为 1 ~ n-1 ,相当于有n-1个抽屉,n个物品

分别计算a[1] + a[2] + …… + a[k] 的和然后取余如果为零则直接输出前k个数,否则寻找余数相同的两个数,假设为i, j (i < j),则a[i+1] + . . . . + a[j] 的和一定能被n整除(原理还没想清楚)

AC代码

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 using namespace std;
 5 int a[10005];
 6 int mod[10005];
 7 int mark[10005];
 8
 9 int main()
10 {
11     int n;
12     bool flag = false;
13     cin >> n;
14     memset(mod, 0, sizeof(mod));
15     memset(mark, 0, sizeof(mark));
16     for(int i = 1; i <= n; i++)
17     {
18         cin >> a[i];
19         mod[i] = (mod[i-1] + a[i]) % n;
20     }
21
22     for(int i = 1; i <= n; i++)
23     {
24         if(mod[i] == 0)
25         {
26             flag = true;
27             cout << i << endl;
28             for(int j = 1; j <= i; j++)
29                 cout << a[j] << endl;
30             break;
31         }
32     }
33
34     if(!flag)
35     {
36         for(int i = 1; i <= n; i++)
37         {
38             if(mark[mod[i]] == 0)
39                 mark[mod[i]] = i;
40             else
41             {
42                 cout << i -mark[mod[i]] << endl;
43                 for(int j = mark[mod[i]]+1; j <= i; j++)
44                     cout << a[j] << endl;
45
46                 break;
47             }
48         }
49     }
50
51     return 0;
52 }

原文地址:https://www.cnblogs.com/ruruozhenhao/p/8719321.html

时间: 2024-10-09 02:13:59

B - 抽屉 POJ - 2356 (容斥原理)的相关文章

Find a multiple POJ - 2356 容斥原理(鸠巢原理)

1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 这道题给你了n个数,让你找这n个数中有没有几个数的和是n的倍数 6 7 题解: 8 你循环遍历一遍这n个数,如果某个数是n的倍数,那就输出一个1再输出这个数 9 如果没有的话,那就对这n个数求一下求前缀和. 10 1.在循环遍历一遍这个前缀和,如果某个数是n的倍数,那就输出i,再循环打印出1到i的

C - 抽屉 POJ - 3370 (容斥原理)

Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoi

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">&