[POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]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 (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.
Solution
1.本题与Find a multiple(题解随笔:http://www.cnblogs.com/COLIN-LIGHTNING/p/8481478.html)基本相同,只需多组数据处理,数据一定要先全部读完再处理;
2.由于n<m,那么至少有两个邻居给的糖果数前缀和关于孩子数同余,取模后相同的前缀和后出现的地址减去前面的地址即可;
3.打一个地址标记,记录模值的第一次出现地址,当第二次出现同一模后前缀和时,使l=第一次出现地址,r=第二次出现地址,区间[l,r]即为所求,注意题目要求输出的是可能的一组地址;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int i,j,l,r,t[100001]={},a[100001]={},sum[100001]={};
void cul(int n,int m){ //分组处理数据;
l=0;
memset(t,-1,sizeof(t));
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
t[0]=0;
for(i=1;i<=m;++i) scanf("%d",&a[i]);
for(i=1;i<=m;++i){
sum[i]=(sum[i-1]+a[i])%n;
if(t[sum[i]]!=-1){
l=t[sum[i]];
r=i;
break;
}
else t[sum[i]]=i; //计录模值首次出现的地址;
}
for(i=l+1;i<=r;++i)printf("%d ",i); //注意输出地址;
printf("\n");
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
while(n!=0){
cul(n,m);
scanf("%d%d",&n,&m);
}
return 0;
}
有关鸽巢原理可以参考我的博客:http://www.cnblogs.com/COLIN-LIGHTNING/p/8439555.html
[POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)的更多相关文章
- POJ3370&HDU1808 Halloween treats【鸽巢原理】
题目链接: id=3370">http://poj.org/problem?id=3370 http://acm.hdu.edu.cn/showproblem.php?pid=1808 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...
- [HDU1205]吃糖果 题解(鸽巢原理)
[HDU1205]吃糖果 Description -HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次 ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
随机推荐
- 201621123037 《Java程序设计》第13周学习总结
作业13-网络 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 其他: 网络编程:由客户端和服务器组成 - 服务器端 第一 ...
- PHP关于传众多参数还是传上下文对象的性能测试
在开发微信公众平台平台的过程中,有这么几个参数总是需要传来传去,$userOpenId,$message,$time. 在整个程序的运行过程中,为了函数方便的处理,将这三个变量一直放在参数列表里.关于 ...
- 转载:java程序调用内存的变化过程
前文知道了java程序运行时在内存中的大概分布,但是对于具体程序是如何运行的,看到一篇文章,直接转载过来. (一)不含静态变量的java程序运行时内存变化过程分析 代码: package oop; / ...
- 第84天:jQuery动态创建表格
jQuery动态创建表格 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- malloc与free函数用法
在C里,内存管理是通过专门的函数来实现.另外,为了兼容各种编程语言,操作系统提供的接口通常是 C 语言写成的函数声明 (Windows 本身也由C和汇编语言写成). 1 分配内存 malloc 函数 ...
- bzoj1318[spoj 744] Longest Permutation
题意 给出一个长度为n的,所有元素大小在[1,n]的整数数列,要求选出一个尽量长的区间使得区间内所有元素组成一个1到区间长度k的排列,输出k的最大值 n<=1e5 分析 不会做,好菜啊.jpg ...
- Windows与VMware中的CentOS系统互通访问
[步骤01]设置 CentOS 虚拟机-网络适配器为“桥接模式(直接连接物理网络),复制物理网络连接状态” [步骤02]配置虚拟网卡 [步骤03]配置 CentOS 网络 [步骤04]测试 windo ...
- linux内核分析 第三周 构造一个简单的Linux系统MenuOS
一.计算机的三个法宝 存储程序计算机,函数调用堆栈,中断二.操作系统的两把剑:1.中断上下文的切换,保存现场和恢复现场2.进程上下文的切换. 三.linux内核源代码的分析: ·arch/目录保存支持 ...
- position:fixed 相对父元素定位
position:fixed是对于浏览器窗口定位的,要实现相当于父元素定位,可以这样: 不设置fixed元素的top,bottom,left,right,只设置margin来实现. 这种方法本质上fi ...
- Java配置jdk图文教程
1.计算机 ==>右键属性 2.高级系统设置 3.环境变量 4.找path变量 5.找到jdk的bin目录并复制路径到path环境变量里(jdk5.0以后就可以只配path路径了). 6.pat ...