• 【鸽巢原理】Halloween treats HDU


    题目

    链接:https://acm.hdu.edu.cn/showproblem.php?pid=1808

    very 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 avoid conflicts, the children have decided they will put all sweets together and then divide them evenly among themselves. From last year's experience of Halloween they know how many sweets they get from each neighbour. Since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbours to visit, so that in sharing every child receives the same number of sweets. They will not be satisfied if they have any sweets left which cannot be divided.

    Your job is to help the children and present a solution.

    输入描述

    The input contains several test cases.
    The first line of each test case contains two integers c and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains n space separated integers a1 , ... , an (1 ≤ ai ≤ 100000 ), where ai represents the number of sweets the children get if they visit neighbour i.

    The last test case is followed by two zeros.

    输出描述

    For each test case output one line with the indices of the neighbours the children should select (here, index i corresponds to neighbour i who gives a total number of ai sweets). If there is no solution where each child gets at least one sweet, print "no sweets" instead. Note that if there are several solutions where each child gets at least one sweet, you may print any of them.

    样例

    样例输入

    4 5

    1 2 3 7 5

    3 6

    7 11 2 5 13 17

    0 0

    样例输出

    3 5

    2 3 4

    题意

    给你n个正整数A,和一个正整数c,问,能否从A中取出不小于1个数加起来被c整除,如果能则输出他们的编号

    题解

    前置知识:鸽巢原理,鸽巢原理简单的讲就是 n个空位放n+1个东西,那至少会有一个空位会放两个东西。

    易知取模有个性质x%m+y%m=x%m,则有y%m=0;

    我们可以利用这个性质,寻找到,在A中两组互不向交的子集x,y,满足sumx%c+sumy%c=sumx%c,那就有sumy%m=0,所以集合y就是一个解。

    集合x总共有(n-1)*n/2种组合,确定x的长度为a后y总共有(n-a-1)*(n-a)/2种选择

    设a为1,y有(n-2)*(n-1)/2种组合,对应的(x+y)集合也有(n-2)*(n-1)/2种组合

    设(n-2)*(n-1)/2+1>=n即 n^2-6*n+3>=0(n>=1)

    当n>=2时不等式成立,根据鸽巢原理,设对m取模后的数为空位,每个A的子集的和为一个物品,且因为空位0为题目所需求答案,即sum%c=0,所以我们去掉该情况,即有c-1个空位

    由于c<=n,取最大,c=n,由上式得,当n>=2时 n^2-6*n+3>=0恒成立,所以(n-2)*(n-1)/2+1>=c-1 (1 ≤ c ≤ n ) 恒成立。

    综上所述,当a取1时,所有的物品数恒大于空位数,根据鸽巢原理,若所有子集对c取模不等于0那么必有一对子集的和对c取模相等,

    所以,x%c+y%c=(x+y)%c,一定有一对x%c=(x+y)%c,即x%c+y%c=x%c,由上面的结论得,集合y就是一个解,由上证明知,在题目条件的限制下必有解。

    本题有非常多种的构造方式,ac代码是我是使用另一种好写的构造方式,原理相同,同时也正好满足,c<=n,但实际上我觉得c更大点应该也有构造方式能处理,ac代码使用的是前缀和组合的方式,sum[l]%c=sum[r]%c,此时有,sum[l]%c-sum[r]%c=0,所以(l,r]是一个解,sum[l]%c=sum[r]%c用同样的方法也能证明,必定存在。

    AC代码

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<ctime>
    #include<string>
    #include<vector>
    #include<map>
    #include<list>
    #include<set>
    #include<stack>
    #include<bitset>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll> pii;
    typedef pair<ll, ll> pll;
    const ll N = 3e5 + 5;
    const ll mod = 998244353;
    const ll INF = 0x3f3f3f3f;
    const ll INF64 = 0x3f3f3f3f3f3f3f3f;
    const double gold = (1 + sqrt(5)) / 2.0;
    const double PI = acos(-1);
    const double eps = 1e-8;
    ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
    ll pow(ll x, ll y, ll mod) { ll ans = 1; while (y) { if (y & 1)ans = (ans * x) % mod; x = (x * x) % mod; y >>= 1; }return ans; }
    ll pow(ll x, ll y) { ll ans = 1; while (y) { if (y & 1)ans = (ans * x) % mod; x = (x * x) % mod; y >>= 1; }return ans; }
    ll inv(ll x) { return pow(x, mod - 2); }
    
    int sum[N];
    int pos[N];
    int main() {
    
        int n, c;
        while (scanf("%d%d", &c, &n), n + c) {
            
            int l = 1, r=1;
            for (int i = 1; i <= n; i++) {
                scanf("%d", sum + i);
                sum[i] = (sum[i] + sum[i - 1]) % c;
                pos[i-1] = 0;
            }
            for (int i = 1; i <= n; i++) {
                if (pos[sum[i]]) {
                    l = pos[sum[i]];
                    r = i;
                    break;
                }
                pos[sum[i]] = i;
            }
            for (int i = l + 1; i <= r; i++) {
                printf("%d ", i);
            }
            printf("
    ");
        }
    
    
    
    }

    鸽巢原理相关题目 hdu 1205/3183/5776

  • 相关阅读:
    ubuntu sudo 号令有效处理
    shell脚本的主笔、存储、实行
    Oracle 11gR1中细粒度访谒收集处事(4)
    Solaris效力经管工具 SMF快速入门指南(1)
    Solaris 10完成宁静Kerberos身份验证(2)
    高质量C /C编程指南第2章 程序的版式
    Solaris 10下设置EJB 3.0景遇(1)
    高质量C /C编程指南序言
    Java学习之路:不走弯路,就是捷径 www.51testing.com
    两个结构体可以直接赋值吗? 回复 "JohnsonAnother" 的问题
  • 原文地址:https://www.cnblogs.com/komet/p/15108430.html
Copyright © 2020-2023  润新知