2356 Find a multiple 抽屉原理 给定n个数,求其中的任意一个子集满足集合中的每个元素值加和正好是n的倍数

本文探讨了一个经典的子集求和问题,即寻找一组数中能满足其和为给定整数N倍数的子集。文章通过巧妙地利用抽屉原理提供了一种高效的O(n)算法解决方案。
Find a multiple
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2547 Accepted: 1109 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
一个经典的问题:给定n个数,求其中的任意一个子集满足集合中的每个元素值加和正好是n的倍数。刚开始怎么也没有思路,因为n很大,直接搜索显然是不行的。后来在组合数学书上找到了这个例题(晕,之前白看了,居然把这个经典的题目都给忘了),是抽屉原理的典型应用。
  假定n个数为a1,a2,...,an,前n项和分别是S1、S2、...、Sn,那么如果有一个Si模n是0,就是答案,否则,n个数模n的余数只能在1到n - 1之间,把余数作为抽屉,显然n个数放到n - 1个抽屉里面,肯定有两个数余数相等,这样取它们的差就得到了结果,算法复杂度是O(n)的。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[11001],vis[11001],s[11001];
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        int pos,len=0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            s[i]=(s[i-1]+a[i])%n;
            if(len) continue;//已经找到解
            int mod=s[i];
            if(mod==0)
            {
                pos=1,len=i;
                continue;
            }
            if(vis[mod]==0)
            {
                vis[mod]=i;
            }
            else
            {
                len=i-vis[mod];
                pos=vis[mod]+1;
            }
        }
        printf("%d/n",len);
        for(int i=pos;i<pos+len;i++) printf("%d/n",a[i]);
    }
    return 0;
}
c++题解,代码没有注释。 B 疲配 作者 2025北京市赛 单位 北京理工大学 给定一个二分图,其中左右两侧各包含 n 个顶点。图中每条边均有一个颜色,颜色可以用一个整数表示,范围在 1 到 k 之间。 对于任意颜色子集 S⊆{1,2,…,k},我们称它为好的,当且仅当存在一组完美匹配,使得该匹配使用的边的颜色恰好为 S 。具体来说,所寻找的完美匹配需要满足以下两个条件: 匹配中的所有边颜色均来自 S; 对于 S 中的任一颜色 c,匹配中至少存在一条边的颜色为 c。 现在,你可以修改至多一条边的颜色为这条边原来颜色的相邻颜色。对于每个颜色子集,你想知道是否存在一种修改方案,使得修改后这个颜色子集是好的。称颜色 x 颜色 y 相邻,当且仅当 ∣x−y∣=1 或 ∣x−y∣=k−1 。 请对每个颜色子集 S 输出相应的判定结果。 输入格式 每个测试文件包含多组测试数据。第一行包含测试数据的组数 T (1≤T≤50) 。每组测试数据的格式如下。 第一行三个整数 n,m,k (1≤n≤50,1≤m≤n 2 ,1≤k≤10),分别代表二分图的点数、边数颜色数量。 接下来 m 行,每行三个整数 u,v,c (1≤u,v≤n,1≤c≤k),表示有一条边连接左部第 u 个点右部第 v 个点,其颜色为 c 。保证图中不存在重边。 在每个测试文件内,保证所有测试数据的 2 k 之不超过 2048 。 输出格式 对于每组数据,输出一行 2 k 个字符。第 i 个字符代表如下颜色集合 S 的答案:对于 j∈[1,k] ,如果 i−1 的二进制表示中从低到高第 j 位为 1 ,则 j∈S ,否则 j∈ / S 。对于这个集合 S ,如果至多修改一条边为其相邻颜色后存在合法的完美匹配,则输出"1",否则输出"0"。 样例输入1 2 3 5 2 1 2 1 2 1 1 3 3 2 3 2 1 1 3 1 5 12 3 1 2 1 1 3 2 1 5 1 2 4 3 2 3 2 2 2 3 3 1 3 3 5 1 4 2 2 4 4 1 5 3 3 5 5 1 样例输出1 0101 00010111 代码长度限制 64 KB 时间限制 2000 ms 内存限制 512 MB 栈限制 131072 KB
最新发布
08-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值