POJ 2356 Find a multiple 神题 传说中的经典.
題目
Description
The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input
The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output
In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
5
1
2
3
4
1
Sample Output
2
2
3
也就是說,在n個數里取一些數使它們的和是n的倍數,輸出取的數的個數以及每個數.如果無法取出則輸出0.
題解
我們寫過指數暴力,打過搜索,因為數據范圍加老爺機跪掉了.我不得不看了看題解.
確實精彩.我們對數列求前綴和,如果前i個的和對n取模為0,則直接輸出1-i的數,否則因為前綴和取模有n個值,但只有n-1種可能(沒有0),所以必然存在2個前綴和是相同的,直接輸出這兩個位置之間的所有數即可.小學學的抽屜原理,到這里就忘了.
總結
以上是生活随笔為你收集整理的POJ 2356 Find a multiple 神题 传说中的经典.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java:打包成jar包
- 下一篇: DEVC++关于Windows-sock