poj 3370 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 first line of each test case contains two integers c and n ( ≤ c ≤ n ≤ ), the number of children and the number of neighbours, respectively. The next line contains nspace separated integers a1 , ... , an ( ≤ ai ≤ ), 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
Sample Output
Source
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
ll n,m;
ll sum[N];
ll vis[N];
ll a[N];
ll tmp[N];
int main()
{
while(scanf("%I64d%I64d",&n,&m)==){
if(n== && m==){
break;
}
memset(sum,,sizeof(sum));
for(ll i=;i<=m;i++){
//ll x;
scanf("%I64d",&a[i]);
sum[i]=sum[i-]+a[i];
} memset(vis,,sizeof(vis));
memset(tmp,,sizeof(tmp));
for(ll i=;i<=m;i++){
ll x=sum[i]%n;
if(vis[x]){
ll y=tmp[x];
//printf("%d\n",i-y);
for(ll j=y+;j<i;j++){
printf("%I64d ",j);
}
printf("%d\n",i);
break; }
if(x==){
//printf("%d\n",i);
for(ll j=;j<i;j++){
printf("%I64d ",j);
}
printf("%d\n",i);
break;
}
vis[x]=;
tmp[x]=i;
} }
return ;
}
poj 3370 Halloween treats(鸽巢原理)的更多相关文章
- POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- [POJ 3370] Halloween treats
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7143 Accepted: 2641 ...
- 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
基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
- POJ3370&HDU1808 Halloween treats【鸽巢原理】
题目链接: id=3370">http://poj.org/problem?id=3370 http://acm.hdu.edu.cn/showproblem.php?pid=1808 ...
随机推荐
- 提高xshell使用效率
1.快速命令集. 2.鼠标复制粘贴设置. 3.配色方案. 4.esc切换到英文输入. 设置入口:
- 一个简单的面试题 很多人也会懵 i++ 和++i的区别
以下分别输出i的值分别为多少 NSInteger i = 0 ; NSLog(@"%ld",i++); NSLog(@"%ld",i++); NSLog(@&q ...
- java与.net比较学习系列(2) 基础语言要素
这一篇从最基础的开始对比总结,说起基础语言要素,故名思义,就是学习语言的基础,主要内容包括标识符,关键字和注释.我想从以下几点进行总结,有区别的地方有都使用红色粗体字进行了总结. 1,标识符 2,关键 ...
- 海量路由表能够使用HASH表存储吗-HASH查找和TRIE树查找
千万别! 非常多人这样说,也包括我. Linux内核早就把HASH路由表去掉了.如今就仅仅剩下TRIE了,只是我还是希望就这两种数据结构展开一些形而上的讨论. 1.hash和trie/radix ha ...
- 谈谈UIView的几个layout方法-layoutSubviews、layoutIfNeeded、setNeedsLayout...
最近在学习swift做动画,用到constraint的动画,用到layoutIfNeeded就去研究了下UIView的这几个布局的方法. 下面是做得一个动画,下载地址:https://github.c ...
- Dalvik虚拟机的启动过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8885792 在Android系统中,应用程序进 ...
- Qt之操作Excel
Visual Basic for Applications(VBA)是一种Visual Basic的一种宏语言,主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件. ...
- Java—Integer类
Integer类 Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 该类提供了多个方法,能在 int 类型和 String 类型之间 ...
- oracle日记
win7旗舰版安装oracle11g 64位用sql plus可以登录 但用pl/sql dev报错ORA-12560 小型数据库:access.foxbase 中型数据库:mysql.sqlserv ...
- python-整理--连接MSSQL
环境 : python3.4 / win10 / vs2013 / sqlexpress2014 需要的工具和包 1.freetds包 下载地址 https://github.com/ramiro/f ...