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个集合里相应的元素个数,需要证明至少存在某个ai大于或等于[n/k]。

  

题意:n个不同的元素,任意一个或者多个相加为n的倍数。找到这些元素。第一个输出元素的个数,后面分别输出这些元素。(多种情况输出一组)

  分析:被n求模的余数为 0,1,2,3....n-1    有n个元素,任意几个数的和为n的倍数,那么这些和假设为 a1, a2 ,a3 ..... am 那么m一定大于n

     把余数当做抽屉,一定会有至少一个抽屉有两个元素!就是抽屉原理的形式一。

#include<cstdio>
#include<cstring>

const int maxn = 1e5 + 5;
int num[maxn], hash[maxn], sum[maxn];
int n;

int main()
{
    while (scanf("%d", &n) != EOF){
        memset(hash, 0, sizeof(hash));
        for (int i = 1; i <= n; ++i)
            scanf("%d", &num[i]);

        int t = 1, s = 1;
        for (int i = 1; i <= n; ++i)
        {
            sum[i] = (sum[i - 1] + num[i]) % n;
            if (sum[i] == 0){
                t = i;
                break;
            }
            if (hash[sum[i]] > 0){
                s = hash[sum[i]] + 1;
                t = i;
                break;
            }
            hash[sum[i]] = i;
        }
        printf("%d\n", t - s + 1);
        for (int i = s; i <= t; ++i)
            printf("%d\n", num[i]);
    }
}

原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9902103.html

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

Find a multiple 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 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

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

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的

Find a multiple POJ - 2356【鸽巢原理】

题意: 求在长度为 \(n\) 的数组中选择连续的元素,使其和是 \(n\) 的倍数,输出元素个数和每个元素的值. 分析: ??我们选取一段连续的元素.对原数组求前缀和,并且对 \(n\) 取模,那么结果就会分布在 \([0,n)\) 之间.如果有一个前缀和取模 \(n\) 的结果为 \(0\),那么这个前缀和一定满足条件.否则,我们考虑有 \(n\) 个结果,分布在长度为 \(n-1\) 的区间上,根据鸽巢原理,必然会有两个相同,那么这两个位置之间的元素和就满足条件. 代码: #include

poj 2356 抽屉问题

#include <stdio.h> #include <math.h> #include <assert.h> #include <string.h> #define MAX 10001 //[(物体数-1)÷抽屉数]+1 //给出N 个数,在其中找出m个数,使得m个数的和是N的倍数,输出m以及任意顺序这m个数,只要输出一种情况. /* 可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0, 根据鸽巢原理,一定有两个数的余数相同,两个

Find a multiple 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

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

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