The Little Girl who Picks Mushrooms

来源:互联网 发布:video sharing软件 编辑:程序博客网 时间:2024/06/05 11:32

It’s yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity of 2012 grams. Alice has finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five mountains, she want to bring as much mushrooms as possible home to cook a delicious soup.

Alice lives in the forest of magic. At the entry of the forest of magic, lives three mischievous fairies, Sunny, Lunar and Star. On Alice’s way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.

Somewhere in the forest of magic near Alice’s house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total.

So when Alice get home, what’s the maximum possible amount of mushrooms Alice has? Remember that our common sense doesn’t always hold in Gensokyo. People in Gensokyo belive that 1 kilograms is equal to 1024 grams.

Input
There are about 8192 test cases. Process to the end of file.

The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.

Output
For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).

Sample Input
1
9
4
512 512 512 512
5
100 200 300 400 500
5
208 308 508 708 1108
Sample Output
1024
1024
0
792
Note

In the second sample, if Alice doesn’t pick any mushrooms from the 5-th mountain. She can give (512+512+0)=1024 grams of mushrooms to Sunny, Lunar and Star. Marisa won’t steal any mushrooms from her as she has exactly 1 kilograms of mushrooms in total.

In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms.

In the last sample:

Giving Sunny, Lunar and Star: (208+308+508)=1024
Stolen by Marisa: ((708+1108)-1024)=792

大致题意:一个小女孩,拿着5个袋子去5座山上采蘑菇,每一座山最多能采蘑菇数量不超过2012,分别用不同的袋子去装。然后采完5座山后,需要选择3个袋子,使得其中的蘑菇数量总和为1024的倍数,交给三个仙女,否则需要把这5个袋子都交给仙女。最后还有个小偷会一直偷剩下的蘑菇,每次都拿1024个,直到剩下的蘑菇总数不大于1024.
现在告诉你前n个山已经每个山已经采的蘑菇数,让你决定剩下的5-n座山每座山所采的蘑菇数量,然后输出最后所能剩余的最多的蘑菇数量。

思路:因为每座山所能采的蘑菇数量范围是0到2012,所以当n小于等于3时,我们都能在满足使得其中三个袋子蘑菇总数为1024的倍数的情况下,剩下的两个袋子蘑菇总数也为1024,所以最后剩余的最多蘑菇数量为1024。当n大于3时,再分情况讨论下。

注意:三个袋子蘑菇数总和为0时也可交给仙女(巨坑。。。)

代码如下

#include <iostream>  #include <cstdio>  using namespace std;  int a[10];int main()  {      int n;    while(scanf("%d",&n)!=EOF)    {        int sum=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            sum+=a[i];        }        if(n<=3)        printf("1024\n");        else if(n==5)        {            int ANS=0;            for(int i=1;i<n;i++)            for(int j=i+1;j<=n;j++)            {                int ans=sum-a[i]-a[j];                if(ans%1024==0)                {                    ans=a[i]+a[j];                    while(ans>1024)                    {                        ans-=1024;                    }                    ANS=max(ANS,ans);                }            }            printf("%d\n",ANS);        }        else if(n==4)        {            int ANS=0;            for(int i=1;i<=n;i++)            {                int ans=sum-a[i];                if(ans%1024==0)                ANS=1024;            }            for(int i=1;i<n;i++)            for(int j=i+1;j<=n;j++)            {                int ans=a[i]+a[j];                while(ans>1024)                {                    ans-=1024;                }                ANS=max(ANS,ans);            }            printf("%d\n",ANS);        }    }    return 0;  }