鴿巢原理應用-分糖果 POJ 3370 Halloween treats

來源:互聯網
上載者:User

基本原理:n+1隻鴿子飛回n個鴿籠至少有一個鴿籠含有不少於2隻的鴿子。

很簡單,應用卻也很多,很巧妙,看例題:

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 containsn 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 aisweets).
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 51 2 3 7 53 67 11 2 5 13 170 0

Sample Output

3 52 3 4

Source

Ulm Local 2007

題目大意:糖果平分問題。有c個小孩,n個提供糖果的鄰居,你可以選擇要或不要。現在你只考慮得到的全部糖果能否平分,可能有多種方案,輸出一種即可。

上面的case 1: 結果 2 3 4 也行,總和為12. 輸出一種即可

#include <stdio.h>#include <algorithm>using namespace std;int c,n,neigb[100001];int S;struct Remnant{int h,r; // 下標和餘數}R[100001];bool cmp(const Remnant &a, const Remnant & b){ //按餘數從小到大排序if( a.r == b.r)return a.h < b.h;return a.r < b.r;}int main(){//freopen("in.txt","r",stdin);while(scanf("%d %d", &c, &n) != EOF){if(c==0 && n==0) break;int k=-1,h;S=0;for(int i=0; i<n; i++){scanf("%d",&neigb[i]);S += neigb[i];R[i].r = S%c; //儲存是前i個和 對c的餘數R[i].h = i + 1; //h 為下標if(k == -1 && R[i].r==0 ) k=i;}if(k == -1){sort(R, R+n, cmp);for(int i=0; i<n-1; i++){if(k == -1 && R[i].r == R[i+1].r) {k = R[i].h;h = R[i+1].h;break;}}if(k==-1)printf("no sweets\n");else{for(int i=k+1; i<h; i++)printf("%d ",i);printf("%d\n",h);}}else{for(int i=0; i<k; i++)printf("%d ",i+1);printf("%d\n",k+1);}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.