POJ 3370 Halloween treats(抽屉原理)

来源:互联网 发布:the game awards知乎 编辑:程序博客网 时间:2024/06/04 22:21
Halloween treats
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7879 Accepted: 2866 Special Judge

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

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

The last test case is followed by two zeros.

Output

For each test case output one line with the indices of the neighbours the children should select (here, indexi corresponds to neighbouri who gives a total number ofai 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.

Sample Input

4 51 2 3 7 53 67 11 2 5 13 170 0

Sample Output

3 52 3 4

Source

Ulm Local 2007

题目大意:
    一共有n个数,从其中取一些数使它们的和膜c等于0。

解题思路:
    由抽屉原理可得,此题一定有解,只要我们计算出前缀和%c的值,当两个点的前缀和%c相等使,中间的值之和%c一定等于0。(注意,需要补充一个一个元素都没有的情况)。

附AC代码:
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#define LL long longusing namespace std;const int maxn=100000+5;int a[maxn],sum[maxn],c,n;int vis[maxn];int main(){    while(~scanf("%d%d",&c,&n)&&(c||n))    {        memset(vis,-1,sizeof vis);//初始化        int l=0,r=0;        bool flag=false;        vis[0]=0;//补充0        for(int i=1;i<=n;++i)        {            scanf("%d",&a[i]);            if(i==1)                sum[i]=a[i]%c;            else sum[i]=(sum[i-1]+a[i])%c;//计算前i项和%c的值            if(!flag&&vis[sum[i]]==-1)//如果没出现过则存下下标                vis[sum[i]]=i;            else if(!flag&&vis[sum[i]]!=-1)//这个数前面出现过,则这次出现和上次出现之间的数的和%c==0            {                l=vis[sum[i]];                r=i;                flag=true;            }        }        for(int i=l+1;i<=r;++i)//将其中的下标全部输出        {            printf("%d",i);            if(i<r)                putchar(' ');        }        putchar('\n');    }        return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩吃饭少消瘦怎么办 小孩吃饭少挑食怎么办 小孩吃饭太少怎么办 孩子不正经吃饭怎么办 月经少卵巢早衰怎么办 四个月婴儿厌食怎么办 孩子读书成绩差怎么办 小孩学习记不住怎么办 儿童不爱吃菜怎么办 10儿童不爱吃饭怎么办 儿童不爱吃蔬菜怎么办 婴儿不爱吃饭怎么办啊 初中不爱写作业怎么办 老是不想写作业怎么办 一年级孩子不爱学怎么办 一年级孩子不爱学习怎么办 我不想读书了怎么办 孩子读书读不懂怎么办 一岁多宝宝不吃奶粉怎么办 小孩不讲话怎么办 舌头 看书记不住内容怎么办 看不下去书怎么办 职高读不下去怎么办 小孩不主动说话怎么办 不喜欢自己的儿子怎么办 生了儿子不喜欢怎么办 孩子不愿意去幼儿园怎么办 内向妈妈带孩子怎么办 从小就不爱学习怎么办 孩子老爱玩不爱学习怎么办? 孩子不爱做题怎么办 看书静不下心怎么办 孩子不自觉学习怎么办 不自觉的孩子怎么办 不学习的孩子怎么办 电脑不受老师控制怎么办 儿子不尊重老师怎么办 小孩不喜欢吃蔬菜怎么办 小孩不愿练钢琴怎么办 孩子不喜欢幼儿园老师怎么办 孩子不主动思考怎么办