【鸽巢原理】Halloween treats HDU - 1808

题目

链接: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个东西,那至少会有一个空位会放两个东西。

本题有非常多种的构造方式,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("\n");
    }



}

 

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

posted @ 2021-08-06 15:23  _comet  阅读(59)  评论(0编辑  收藏  举报