POJ 3370 Halloween treats(抽屉原理)
来源:互联网 发布:the game awards知乎 编辑:程序博客网 时间:2024/06/04 22:21
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
题目大意:
一共有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;}
- POJ 3370 Halloween treats(抽屉原理)
- POJ 3370 Halloween treats(抽屉原理)
- POJ 3370 Halloween treats(抽屉原理)
- POJ 3370 Halloween treats(抽屉原理)
- POJ-3370 Halloween treats (抽屉原理)
- poj 3370 Halloween treats(抽屉原理)
- POJ 3370 Halloween treats(抽屉原理)
- POJ 3370 Halloween treats(抽屉原理)
- poj 3370 Halloween treats 【抽屉原理】
- poj 3370 Halloween treats <抽屉原理>
- POJ 3370 Halloween treats(抽屉原理)
- 【POJ】3370 - Halloween treats(抽屉原理 & STL)
- 【Poj】-3370-Halloween treats(抽屉原理&STL)
- POJ3370 Halloween treats 抽屉原理
- 抽屉原理-poj3370-Halloween treats
- poj 3370 Halloween treats(反演定理+鸽巢原理)
- POJ 3370 Halloween treats(鸽巢原理)
- POJ 题目3370 Halloween treats(鸽巢原理)
- TCP、UDP、HTTP、SOCKET
- js中文传值乱码
- sql语句优化原则
- Handler的介绍
- android打包
- POJ 3370 Halloween treats(抽屉原理)
- POJ2096(概率dp)
- CentOS7 安装Mysql5.7(解压缩版)
- 【Python学习笔记】函数式编程:高阶函数sorted
- 第十一篇 ANDROID 系统网络连接和管理机制与架构
- 大数据:Spark性能优化指南 高级篇
- HDU 4277 USACO ORZ DFS
- 【zTree】简单实例与异步加载实例
- 学习Java的第一天