POJ 2355 Find a multiple(组合数学-抽屉原理)

Find a multiple

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5881   Accepted: 2560   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

Ural Collegiate Programming Contest 1999

题目大意:

有n个数,找出一个方案满足:从中选出任意多的数字使得它们的和对n求余为0

解题思路:

用sum[i]记录前 i 项的和。

(1)如果存在某个sum[i]%n==0 ,那么就已经找到了,就是前i项。

(2)如果不存在,则sum[i]%n的取值范围为1~n-1 那么n项sum必然有 sum[i]%n==sum[j]%n,这时候(sum[j]-sum[i])%n=0,也就是 第i+1项到第j项的和对n求余为0,也满足条件了。

解题代码:

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn=11000;
int n,a[maxn],sum[maxn],visited[maxn];

void solve(){
    for(int i=1;i<=n;i++){
        if(sum[i]%n==0){
            printf("%d\n",i);
            for(int t=1;t<=i;t++){
                printf("%d\n",a[t]);
            }
            return ;
        }
    }
    for(int i=0;i<=n;i++) visited[i]=-1;
    for(int i=1;i<=n;i++){
        if(visited[sum[i]%n]!=-1){
            int t=visited[sum[i]%n];
            printf("%d\n",i-t);
            for(t=t+1;t<=i;t++){
                printf("%d\n",a[t]);
            }
        }else{
            visited[sum[i]%n]=i;
        }
    }
}

int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        sum[0]=0;
        for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
        solve();
    }
    return 0;
}

POJ 2355 Find a multiple(组合数学-抽屉原理)

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

POJ 2355 Find a multiple(组合数学-抽屉原理)的相关文章

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 a multiple (鸽巢原理妙用)

题目链接:http://poj.org/problem?id=2356 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

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 抽屉原理

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

Find a multiple POJ - 2356 (抽屉原理)

抽屉原理: 形式一:设把n+1个元素划分至n个集合中(A1,A2,-,An),用a1,a2,-,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2. 形式二:设把nm+1个元素划分至n个集合中(A1,A2,-,An),用a1,a2,-,an表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于m+1. 形式三:设把n个元素分为k个集合A1,A2,-,Ak,用a1,a2,-,ak表示这k个集合里相应的元素个数,需

POJ 3370 Halloween treats(抽屉原理)

题意  有c个小孩 n个大人万圣节搞活动  当小孩进入第i个大人家里时   这个大人就会给小孩a[i]个糖果  求小孩去哪几个大人家可以保证得到的糖果总数是小孩数c的整数倍  多种方案满足输出任意一种 用s[i]表示前i个打人给糖果数的总和  令s[0]=0  那么s[i]共有n+1种不同值  而s[i]%c最多有c种不同值  题目说了c<=n   所以s[i]%c肯定会有重复值了 这就是抽屉原理了   n个抽屉放大于n个苹果   至少有一个抽屉有大于等于2个苹果 就把s[i]%c的取值个数(c

poj2356 Find a multiple(抽屉原理|鸽巢原理)

/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据抽屉原理: 因为有n个数,对n个数取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同, 如果余数出现0,自然就是n的倍数.也就是说,n个数中一定存在一些数的和是n的倍数. 本题的思路是从第一个数开始一次求得前 i(i <= N)项的和关于N的余数sum,并依次记录相应余数的存在状态,