ZOJ 3657The Little Girl..2012長春現場賽C題(簡單思維+細心)

果7發表於2013-08-21
The Little Girl who Picks Mushrooms

Time Limit: 2 Seconds      Memory Limit: 32768 KB

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


               感受:PI隊在十分鐘內就直接A了這題,當時我在看B題,是關於矩陣&|位運算的。小吉吉當時在看C題,但是看了十幾分鐘沒看懂,博博準備敲K題。我跟吉吉就一起看了C題,博博敲了,交上去是TLE,然後想著怎麼節省時間打表或者直接算對數取最接近的值列舉。結果是我們把資料看錯了,紙上是1012顯示不了10^12.瞬間有種崩潰的感覺。。吉吉決定寫C題,但是交上去了卻WA了,然後他就判斷可以給0,還是WA。然後我們就別的題目都不看了,專心搞這兩個。這兩題都是最後二十分鐘A的,太驚險了。覺得我們還是交流地不是很到位。不過BME的實力還是很大的。


            題目大意:給你n(0~5)個數,總共有五堆數,前n堆確定。後面的可以可以任意取。需要找出三堆模上1024給別人,找不出來輸出0,。自己留下兩堆,然後兩堆之和大於1024全部是別人的。主要是判0的情況。


            解題思路:如果n<=3,直接輸出1024.如果n==4,先找三堆能不能組成1024的倍數,注意可以是0,如果可以直接輸出1024.如果不能找出4堆裡面兩堆之和最大的,注意0與1024的轉變。n==5的時候找三堆看是否能組成1024的倍數,注意可以是0.然後另兩堆最大的,注意0與1024的轉變。


            題目地址:The Little Girl who Picks Mushrooms


AC程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
int a[8],n,sum,ans;

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        sum=0,ans=0;
        for(i=1;i<=n;i++)
        {
             scanf("%d",&a[i]);
             sum+=a[i];
        }
        if(n<=3)
            puts("1024");
        else if(n==4)
        {
            int flag=0;
            for(i=1;i<=4;i++)  //3個之和可以組成1024倍數
                if((sum-a[i])%1024==0) //可以給0
                {
                     flag=1;
                     break;
                }
            if(flag) puts("1024");
            else
            {
                int tmp;
                for(i=1;i<=3;i++)
                    for(j=i+1;j<=4;j++)
                    {
                        tmp=a[i]+a[j];
                        if(tmp)  //自己拿到的0不能直接變成1024
                        {
                            tmp%=1024;
                            if(tmp==0) tmp=1024;
                        }
                        if(ans<tmp) ans=tmp;
                    }
                printf("%d\n",ans);
            }
        }
        else  //n為5
        {
            int tmp;
            for(i=1;i<=4;i++)
               for(j=i+1;j<=5;j++)
               {
                   int t=sum-a[i]-a[j];
                   if(t%1024==0)  //可以給0
                   {
                        tmp=a[i]+a[j];
                        if(tmp)  //自己拿到的0不能直接變成1024
                        {
                            tmp%=1024;
                            if(tmp==0) tmp=1024;
                        }
                        if(ans<tmp) ans=tmp;
                   }
               }
            printf("%d\n",ans);
        }
    }
    return 0;
}



             

相關文章