POJ 3370 Halloween treats(抽屉原理)

Problem Description

Every 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.

Input

<p>The input contains several test cases.<br>The first line of each test case contains two integers <strong><i>c</i></strong> and <strong><i>n</i></strong> (<i>1 ≤ c ≤ n ≤ 100000</i>), the number of children and the number of neighbours, respectively. The next line contains <strong><i>n</i></strong> space separated integers <strong><i>a</i><sub>1</sub> , ... , <i>a</i><sub><i>n</i></sub></strong> (<i>1 ≤ a<sub>i</sub> ≤ 100000 </i>), where <strong><i>a</i><sub><i>i</i></sub></strong> represents the number of sweets the children get if they visit neighbour <strong><i>i</i></strong>.</p><p>The last test case is followed by two zeros.</p>

Output

<p>For each test case output one line with the indices of the neighbours the children should select (here, index <strong><i>i</i></strong> corresponds to neighbour <strong><i>i</i></strong> who gives a total number of <strong><i>a</i><sub>i</sub></strong> 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.</p>

Sample Input

4 5 1 2 3 7 5 3 6 7 11 2 5 13 17 0 0

Sample Output

3 5 2 3 4

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
const int maxx=100010;
using namespace std;
int c,n;
long a[maxx],sum[maxx],flag[maxx];
int main()
{
    int i,t;
    while(scanf("%d %d",&c,&n)!=EOF){
            memset(flag,-1,sizeof(flag));t=0;
            flag[0]=0;//注意这是一个坑的点,等于0时有点特别
        if(c==0&&n==0) break;
    sum[0]=0;
        for(i=1;i<=n;i++){
            scanf("%ld",&a[i]);
            sum[i]=(sum[i-1]+a[i])%c;//计算前i项的和。
        }
        for(i=1;i<=n;i++){
            if(flag[sum[i]]==-1){
                flag[sum[i]]=i;
            }
            else {
                long j=flag[sum[i]]+1;
                for(j=flag[sum[i]]+1;j<=i;j++){
                    if(j==flag[sum[i]]+1) printf("%ld",j);
                    else printf(" %ld",j);
                }
//大体意思就是si==sio时,他们中间的项可以整除c,因为两个对c取余相同。
                t=1;
                break;
            }
        }
        if(t==0) printf("no sweets");
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值