ZOJ

/*ZOJ Problem Set - 3657
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
Author: WU, Zejun
Contest: The 2012 ACM-ICPC Asia Changchun Regional Contest
题意:
略
解法:
略
*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int a[6];
int main()
{
    int sum,MAX,flag,i,n,j,k,tmp,ans;
    while(~scanf("%d",&n))
    {
        sum=0;
        MAX=0;
        flag=false;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(n<=3)
            printf("1024
");
        else if(n==4)
        {
            for(i=0; i<n; i++)
            {
                sum-=a[i];
                if(sum%1024==0)
                {
                    flag=true;
                    printf("1024
");
                    break;
                }
                sum+=a[i];
            }
            if(!flag)
            {
                for(i=0; i<n; i++)
                    for(j=i+1; j<n; j++)
                    {
                        if((a[i]+a[j])%1024!=0)
                            tmp=(a[i]+a[j])%1024;
                        else
                        {
                            if(a[i]+a[j]==0)
                                tmp=0;
                            else
                                tmp=1024;
                        }
                        MAX=max(tmp,MAX);
                    }
                printf("%d
",MAX);
            }
        }
        else
        {
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                    for(k=j+1; k<n; k++)
                    {
                        tmp=a[i]+a[j]+a[k];
                        if(tmp%1024==0)
                        {
                            ans=sum-tmp;
                            if(ans%1024!=0)
                                ans=ans%1024;
                            else
                            {
                                if(ans!=0)
                                ans=1024;
                            }
                            MAX=max(MAX,ans);
                        }
                    }
            printf("%d
",MAX);
        }
    }
    return 0;
}
 1 /*二进制枚举法*/
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <string.h>
 5 using namespace std;
 6 int del(int n,int a[]){
 7     int r[5],ct=0,i=0;
 8      while(n){
 9         if(n&1){
10             r[ct++]=i;
11         }
12         n=n>>1;
13         i++;
14      }
15      int t=0,th=0;
16      if(ct==2){
17         for(i=0;i<5;i++)
18          if(i==r[0]||i==r[1]){
19             if(a[i]==-1) t=1024;
20             else t+=a[i];
21         }else{
22             if(a[i]==-1) th=1024;
23             else th+=a[i];
24         }
25        // printf("%d %d
",t,th);
26         if(th%1024==0){
27              while(t>1024)t-=1024;
28              return t;
29         }
30         return 0;
31      }
32     return 0;
33 }
34 int main(){
35    int n;
36    int i,a[10];
37    int ans;
38    while(scanf("%d",&n)!=EOF){
39        for(i=0;i<n;i++)
40           scanf("%d",&a[i]);
41        a[i]=-1;
42       if(n<=3) printf("1024
");
43       else{
44         ans=0;
45         for(i=3;i<=24;i++)
46             ans=max(ans,del(i,a));
47             printf("%d
",ans);
48       }
49    }
50    return 0;
51 }
原文地址:https://www.cnblogs.com/heqinghui/p/3230593.html