zoukankan      html  css  js  c++  java
  • HDU 1808 Halloween treats(抽屉原理)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1808

    Problem 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.
     
    Sample Input
    4 5
    1 2 3 7 5
    3 6
    7 11 2 5 13 17
    0 0
     
    Sample Output
    3 5
    2 3 4
     
    启发题解:http://blog.csdn.net/liwen_7/article/details/8047273
    抽屉原理:http://baike.baidu.com/link?url=qugmIxXpH8G9HJzDYsKlnWOtkPRvzgvLZ_Qjfi7bKG5gyorL8C5Wh5f3dJsp_PCU4MQlBEF7o7KH9URyhOBMX9yYu6_aYR4xG4Wp_Y4ktZca3WODwQ2alV-SDRVJJ-ES
     

     题意: 已知有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的倍数。

     
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<string.h>
     6 #include<cmath>
     7 using namespace std;
     8 #define N 100005
     9 
    10 
    11 struct node
    12 {
    13     int num,r;
    14 }k[N];
    15 
    16 bool cmp(node a,node b)
    17 {
    18     if(a.r==b.r)
    19         return a.num<b.num;
    20     return a.r<b.r;
    21 }
    22 
    23 int main()
    24 {
    25     long long c,a,sum;
    26     int n;
    27     bool flag;
    28     while(~scanf("%lld%d",&c,&n))
    29     {
    30         if(c==0&&n==0)
    31             break;
    32         flag=false;
    33         sum=0;
    34         k[0].num=0;
    35         for(int i=1;i<=n;i++)
    36         {
    37             scanf("%lld",&a);
    38             sum+=a;
    39             k[i].r=sum%c;
    40             k[i].num=i;
    41             if(k[i].r==0&&flag==false)
    42             {
    43                 flag=true;
    44                 printf("1");
    45                 for(int j=2;j<=i;j++)
    46                     printf(" %d",j);
    47                 printf("
    ");
    48             }
    49         }
    50         if(!flag)
    51         {
    52             sort(k+1,k+1+n,cmp);
    53             for(int i=2;i<=n;i++)
    54             {
    55                 if(k[i].r==k[i-1].r)
    56                 {
    57                     printf("%d",k[i-1].num+1);
    58                     for(int j=k[i-1].num+2;j<=k[i].num;j++)
    59                         printf(" %d",j);
    60                     printf("
    ");
    61                     flag=true;
    62                     break;
    63                 }
    64             }
    65         }
    66         if(!flag)
    67             printf("no sweets
    ");
    68     }
    69     return 0;
    70 }
  • 相关阅读:
    LINQ大全。
    李开复回复:为什么很多人进不了Google
    判断输入的是否是数字?
    SQL Server 2005安装详解
    学习asp.net比较完整的流程
    .NET常用网站
    软件设计经典书籍推荐
    Linux 下zip包的压缩与解压
    centos的用户、组权限、添加删除用户等操作的详细操作命令
    CentOS5.5 默认基本服务详解
  • 原文地址:https://www.cnblogs.com/Annetree/p/7095096.html
Copyright © 2011-2022 走看看