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 ...
随机推荐
- 利用select实现IO多路复用TCP服务端
一.相关函数 1. int select(int maxfdp, fd_set *readset, fd_set *writeset, fd_set *exceptset,struct timeva ...
- 数据库日常维护-CheckList_01历史Agent Job执行情况检查
检查Agent Job中日常维护作业或业务作业是否成功,如每天的备份.碎片整理.索引维护.历史备份文件清除等,可利用SSMS工具,通过CDC下面设置好的DB Server List,运行下面脚本一次, ...
- NSCharacterSet 使用说明
NSCharacterSet 和 NSMutableCharacterSet 用面向对象的方式来表示一组Unicode字符,它经常与NSString及NSScanner组合起来使用,在不同的字符上 ...
- 巡检脚本OS+Oracle
巡检脚本 主机巡检脚本:OSWatcher.sh Oracle巡检脚本:ORAWatcher.sh 脚本使用方法 1.建立脚本放置目录 # mkdir /var/collect 2.把脚本ORAWat ...
- Bootstrap框架的学习(一)
一.Bootstrap框架介绍 Bootstrap是一个非常优秀的前端UI框架,一个轻量级的UI前端框架,是基于HTML+CSS+JavaScript的框架. 二.简单介绍 Bootstrap框架是属 ...
- 读书笔记--SQL必知必会--建立练习环境
书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL in 10 Minutes - Fourth Edition> MyS ...
- php后台增删改跳转
php登录页面: <h1>登录界面</h1> <form action="dengluchuli.php" method="post&quo ...
- [原创]django+ldap实现统一认证部分一(django-auth-ldap实践)
前言 接之前我的文章,django+ldap+memcache实现单点登录+统一认证 ,ldap部署相关,ldap双机\LAM配置管理\ldap备份还原,目前来说,我们已经有了高可用性的ldap环境了 ...
- JS魔法堂:不完全国际化&本地化手册 之 拓展篇
前言 最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...
- asp.net core 依赖注入问题
最近.net core可以跨平台了,这是一个伟大的事情,为了可以赶上两年以后的跨平台部署大潮,我也加入到了学习之列.今天研究的是依赖注入,但是我发现一个问题,困扰我很久,现在我贴出来,希望可以有人帮忙 ...