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

本文探讨了一个经典的算法问题,即如何从给定的数字序列中找出一个子序列,使其和为序列长度的倍数。通过使用前缀和与抽屉原理,文章详细解释了两种解决方案,一种使用数组记录前缀和的状态,另一种使用map实现,提供了完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 的倍数

思路:
抽屉原理;用前缀和来做;
详情解释在代码中:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxx=1e5+7;
int a[maxx];
int b[maxx];//存放前n个数取模的前缀
int c[maxx];//记录前缀是否出现
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=(b[i-1]+a[i])%n;//计算前缀和取模
    }
    for(int i=1;i<=n;i++)
    {
        if(b[i]==0)
        {
            printf("%d\n",i);
            for(int j=1;j<=i;j++)
                printf("%d\n",a[j]);
            break;
        }
        if(c[b[i]]==0) c[b[i]]=i;//如果这个前缀没有出现过,那么标记前缀,赋予当前位置信息
        else{//如果前缀出现过,那么从上一次出现位置+1的地方到现在的位置,和一定是n的倍数
            printf("%d\n",i-c[b[i]]);
            for(int j=c[b[i]]+1;j<=i;j++)
            {
                printf("%d\n",a[j]);
            }
            break;
        }
    }
    return 0;
}
当然也可以用map来实现,更方便一点;

思路和上面一模一样,只不过把 c 数组用map给替换了;

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;
const int maxx=1e5+7;
int sum[maxx];
int a[maxx];
map<int,int> mp;//存放每个前缀是否出现过,并且在哪里出现
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        cin >>a[i];
        sum[i]=(sum[i-1]+a[i])%n;
    }
    for(int i=1;i<=n;i++)
    {
        if(sum[i]==0)
        {
            cout <<i<<endl;
            for(int j=1;j<=i;j++)
            {
                printf("%d\n",a[j]);
            }
            break;
        }
        else if(!mp[sum[i]])
        {
            mp[sum[i]]=i;
        }
        else
        {
            cout <<i-mp[sum[i]]<<endl;
            for(int j=mp[sum[i]]+1;j<=i;j++)
            {
                printf("%d\n",a[j]);
            }
            break;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值