POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats
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 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 Sample Output 3 5 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
本文地址:http://www.cnblogs.com/BlackStorm/p/5245868.html
POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理的更多相关文章
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...
- POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- Halloween treats HDU 1808 鸽巢(抽屉)原理
Halloween treats Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [POJ 3370] Halloween treats
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7143 Accepted: 2641 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
随机推荐
- lua解析赋值类型代码的过程
我们来看看lua vm在解析下面源码并生成bytecode时的整个过程: foo = "bar" local a, b = "a", "b" ...
- Logstash时区、时间转换,message重组
适用场景 获取日志本身时间 日志时间转Unix时间 重组message 示例日志: hellow@,@world@,@2011-11-01 18:46:43 logstash 配置文件: input{ ...
- ElasticSearch 5学习(4)——简单搜索笔记
空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...
- Windows Live Writer 在线安装失败的解决方法。
这里提供一种解决方法:下载离线安装包,我这个版本是2011的.大家有兴趣的话可以下载一下: http://wl.dlservice.microsoft.com/download/8/3/D/83D75 ...
- visual formatting model (可视化格式模型)【持续修正】
概念: visual formatting model,可视化格式模型 The CSS visual formatting model is an algorithm that processes a ...
- JavaScript原型链分析
Js之原型链 1.object 与 Object 1.object是类型,表示对象类型 2.Object是函数 2.js成员的访问规则 o.方法( ...
- Android Studio同时打开多个项目
Android Studio的默认设置是打开第二个项目时,第一个项目就被自动关闭了,如果要同时打开多个项目,可以点击File->Settings,对Project Opening进行下面的设置: ...
- 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...
- canvas调用scale或者drawImage图片操作后,锯齿感很明显的解决
<script type="text/javascript"> //解决canvas画画图片 var mengvalue = -1; var phoneWidth = ...
- angular2 service component
[component 需要通过 service 提供的接口 得到一些数据.这是最佳实践.] [由于 有 component 和 service 两个语义,所以出现了下面两种办法] 一,[service ...