HDU 1808 Halloween treats(抽屉原理)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1808
Your job is to help the children and present a solution.
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.
题意: 已知有n户人,每户会给小孩们一定数量的糖果(会给的数量假设小孩都已知),求小孩挑选哪几户人家,所得糖果总数能够使每个小孩得到相同数量的糖果,即是小孩数目的倍数?
思路: 设a1、a2……am是正整数的序列,则至少存在整数k和l,(1<=k<l<=m),使得和a(k+1) + a(k+2) + ... ... +al是m的倍数。
证明: x%m的余数有(m-1)中可能,即设有(m-1)个鸽巢,设sn代表(a1+a2+...+an)则m个sn产生m个余数,根据鸽巢原理,一定至少有两个s的余数相等,将这连个s想减,中间a(k+1) + a(k+2) + ... ... +al一定是m的倍数。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cmath>
using namespace std;
#define N 100005 struct node
{
int num,r;
}k[N]; bool cmp(node a,node b)
{
if(a.r==b.r)
return a.num<b.num;
return a.r<b.r;
} int main()
{
long long c,a,sum;
int n;
bool flag;
while(~scanf("%lld%d",&c,&n))
{
if(c==&&n==)
break;
flag=false;
sum=;
k[].num=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a);
sum+=a;
k[i].r=sum%c;
k[i].num=i;
if(k[i].r==&&flag==false)
{
flag=true;
printf("");
for(int j=;j<=i;j++)
printf(" %d",j);
printf("\n");
}
}
if(!flag)
{
sort(k+,k++n,cmp);
for(int i=;i<=n;i++)
{
if(k[i].r==k[i-].r)
{
printf("%d",k[i-].num+);
for(int j=k[i-].num+;j<=k[i].num;j++)
printf(" %d",j);
printf("\n");
flag=true;
break;
}
}
}
if(!flag)
printf("no sweets\n");
}
return ;
}
HDU 1808 Halloween treats(抽屉原理)的更多相关文章
- uva 11237 - Halloween treats(抽屉原理)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/37612503 题目链接:uva 11237 ...
- 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 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- HDU 1808 Halloween treats
Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
- HDU 5776 sum(抽屉原理)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776 Problem Description Given a sequence, you're ask ...
- hdu 1205 吃糖果 (抽屉原理<鸽笼原理>)
吃糖果Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submissi ...
- Halloween treats HDU 1808 鸽巢(抽屉)原理
Halloween treats Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
随机推荐
- html和jsp的区别及优缺点
♥ HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性语言呢?因为,只要你有一个浏览器那么它就可以正常显示 ...
- element upload 一次性上传多张图片(包含自定义上传不走action)
最重要的都圈出来了
- leetcode-algorithms-28 Implement strStr()
leetcode-algorithms-28 Implement strStr() mplement strStr(). Return the index of the first occurrenc ...
- 安卓——Activity生命周期、
在xml 设计页面添加标签 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmln ...
- AWS EC2 MySQL迁移到RDS案例
Amazon Relational Database Service (Amazon RDS) 是一种Web 服务,可让用户更轻松地在云中设置.操作和扩展关系数据库.它可以为行业标准关系数据库提供经济 ...
- Eclipse集成Maven进行开发
Eclipse上Maven环境配置使用 (全) Eclipse EE+MAVEN配置以及POM.XML的使用 Eclipse报错:Referenced classpath provider does ...
- The Doors
The Doors You are to find the length of the shortest path through a chamber containing obstructing w ...
- maven-assembly-plugin
<build> <finalName>detail</finalName> <plugins> <plugin> <artifactI ...
- Eclipse Mars-Ant无法使用jre1.6的问题
https://www.jianshu.com/p/24b9517d7f43 Eclipse 升级到Mars 4.5.1版本后,老项目使用jdk1.6,通过ant编译jar时,会遇到如下问题: 使用j ...
- hashlib 库
hashlib 库 hash 是一种算法,用来接收一系列数据,经过计算后得到一个hash值 hash值的三大特征: 1. 如果传入的数据一样,得到的hash值一样 2. 只要采用的hash算法固定,无 ...