Halloween treats
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7644   Accepted: 2798   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 and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains n space separated integers a1 , ... , an (1 ≤ ai ≤ 100000 ), where ai represents the number of sweets the children get if they visit neighbour i.

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, index i corresponds to neighbour i who gives a total number of ai 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 5
1 2 3 7 5
3 6
7 11 2 5 13 17
0 0

Sample Output

3 5
2 3 4

Source

题目大意

  有$c$个孩纸,$n$个邻居,给出访问每个邻居会得到的糖果数量,在不考虑得到糖果总数量的情况下,试给出一种访问邻居的方案,使得到的糖果能被孩纸们完全均分

基本思路

  1、此题与POJ2356是一样的思路,此题题解可看这里,下面只作简略分析。

  2、此题数据保证$c\leqslant n$,因此由鸽巢原理,在$mod\ n$环下,令$S(n)=a_1+a_2+\cdots+a_n$,有$S(n_1)=S(n_2)$,即$[S(n_2)-S(n_1)]\%n=a_{n_1+1}+a_{n_1+2}+\cdots+a_{n_2}=0$,所以一定有解。

  3、若此题不保证$c\leqslant n$的约束,则可以出现无解的情况,比如有$5$个孩纸,但只有$1$个邻居$1$颗糖。

  4、若此题不保证$a_i\geqslant 1$的约束,则可以出现某个邻居不给糖的情况($a_i=0$,给$0$颗糖),这样对结果不影响,但输出时可以忽略掉$0$的项。

  5、如果无尽WA,请检查是否输出的是邻居的编号而不是糖的数目。

  6、如果无尽TLE,这是因为此题有多组数据,请试试输出最小的解

AC代码

 #include <stdio.h>
#include <string.h> const int maxn=;
int c, n;
int sw[maxn], sum[maxn], sgn[maxn]; int main() {
while(~scanf("%d%d",&c,&n)&&c) {
int l=, r=maxn;
memset(sgn, 0xff, sizeof(sgn)); sgn[]=;
for(int i=; i<=n; i++) {
scanf("%d", sw+i);
sum[i]=(sum[i-]+sw[i])%c;
if(!~sgn[sum[i]]) {
sgn[sum[i]]=i;
}else if(i-sgn[sum[i]]<r-l) {
l=sgn[sum[i]];
r=i;
}
}
for(int i=l+; i<r; i++)
printf("%d ", i);
printf("%d\n", r);
}
return ;
}

POJ 3370

——本文原创by BlackStorm,转载请注明出处。

本文地址:http://www.cnblogs.com/BlackStorm/p/5245868.html

POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理的更多相关文章

  1. [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)

    [POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...

  2. POJ 3370 Halloween treats(抽屉原理)

    Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...

  3. POJ 3370 Halloween treats 鸽巢原理 解题

    Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...

  4. POJ 3370 Halloween treats( 鸽巢原理简单题 )

    链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...

  5. Halloween treats HDU 1808 鸽巢(抽屉)原理

    Halloween treats Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  7. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

  8. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  9. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

随机推荐

  1. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  2. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  3. 浅析MySQL复制

    MySQL的复制是基于binlog来实现的. 流程如下 涉及到三个线程,主库的DUMP线程,从库的IO线程和SQL线程. 1. 主库将所有操作都记录到binlog中.当复制开启时,主库的DUMP线程根 ...

  4. Vertica 7.1安装最佳实践(RHEL6.4)

    一.前期准备工作 1.1各节点IP和主机名 1.2上传脚本并设定环境变量 1.3添加信任 1.4前期准备检查并调整 二.Vertica安装 三.集群性能评估 一.前期准备工作: 1.1各节点IP和主机 ...

  5. 安装完成后在命令行运行bash时报错0x80070057

    在命令运行bash 提示如下: 解决方法,不启用旧版本控制台: 右键命令提示栏 打开属性,把勾选去掉如下图红色边框标识: 然后重启,就可以使用,也包括可以打开Bash on Unbuntu on Wi ...

  6. 使用PD(PowerDesigner)图如何快速生成创建数据库表的SQL脚本

    打开PD软件: 1.新建概念模型(conceptual Data Model) File-->New Model-->Conceptual Data Mode 或者点击工作区,右键--&g ...

  7. C#开发微信门户及应用(3)--文本消息和图文消息的应答

    微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为计划的安排事情之一了.本系列文章希望从一个循序渐进的角度上,全面介绍微 ...

  8. Day01 login module

    知识点:模块导入  变量赋值的两种形式  格式化输出  for循环  if...else 嵌套 #!C:\Program Files\Python35/bin # -*- conding:utf-8 ...

  9. 记录一次bug解决过程:eclipse Installed JREs 配置引出的问题

    一 总结 eclipse Installed JREs 配置引出的问题:编译以来JDK,不是JRE spring boot内嵌tomcat运行程序,tomcat:run 二 Bug描述:eclipse ...

  10. logstash+elasticsearch+kibana管理日志(安装)

    logstash1.先安装jdk2.wget https://download.elastic.co/logstash/logstash/logstash-2.4.0.tar.gz tar -xzvf ...