ZOJ 3657 The Little Girl who Picks Mushrooms

The Little Girl who Picks Mushrooms

Time Limit: 2000ms

Memory Limit: 32768KB

This problem will be judged on ZJU. Original ID: 3657
64-bit integer IO format: %lld      Java class name: Main

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

Source

The 2012 ACM-ICPC Asia Changchun Regional Contest

Author

WU, Zejun

解题:题目不好理解。苦逼的Alice.

采姑娘的小蘑菇。话说有五座山,输入n,表示已经踩过几座山了。剩余的几座山还是会去采的。如果n少于3.那么任选已经采过的两座山,从未选的山里面采取一些蘑菇,使得三山的蘑菇量刚好是1024的整数倍。

如果n == 4.那么,如果已采的4座中,有3座山的蘑菇量和刚好是1024的整数倍。那么,剩余的已采的那座山和未采的那座山,一定可使剩余的蘑菇量为1024.

如果已采的4座中,找不到三山的蘑菇量和为1024的整数倍,那么,只好选择其中的两座山,与未采的那座山,求和,使得这三山的蘑菇量为1024.前面采过的剩余的那两座山的蘑菇量即为剩余的(当然要不断-1024,使得不多于1024)。

如果n == 5,那么只看五选三山中是否存在三山蘑菇量和为1024,如有,则剩余的两山蘑菇和为剩余的蘑菇。(while(x > 1024)   x -= 1024;) .否则,0.

以上求解,均取最优的那个。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int d[6],n,ans,sum;
18 bool flag;
19 int main(){
20     while(~scanf("%d",&n)){
21         sum = ans = 0;
22         for(int i = 1; i <= n; i++){
23             scanf("%d",d+i);
24             sum += d[i];
25         }
26         if(n <= 3) {puts("1024");continue;}
27         if(n == 5){
28             for(int i = 1; i <= n; i++){
29                 for(int j = i+1; j <= n; j++){
30                     for(int k = j+1; k <= n; k++){
31                         int tmp  = d[i]+d[j]+d[k];
32                         if(tmp%1024 == 0){
33                             tmp = sum - tmp;
34                             while(tmp > 1024) tmp -= 1024;
35                             ans = max(ans,tmp);
36                         }
37                     }
38                 }
39             }
40             printf("%d\n",ans);
41             continue;
42         }
43         flag = false;
44         for(int i = 1; i <= n; i++){
45             for(int j = i+1; j <= n; j++){
46                 for(int k = j+1; k <= n; k++){
47                     int tmp = d[i]+d[j]+d[k];
48                     if(tmp%1024 == 0){
49                         flag = true;
50                         break;
51                     }
52                 }
53             }
54         }
55         if(flag){puts("1024");continue;}
56         for(int i = 1; i <= n; i++){
57             for(int j = i+1; j <= n; j++){
58                 int tmp = d[i]+d[j];
59                 while(tmp > 1024) tmp -= 1024;
60                 ans = max(ans,tmp);
61             }
62         }
63         printf("%d\n",ans);
64     }
65     return 0;
66 }

时间: 2024-08-01 10:41:51

ZOJ 3657 The Little Girl who Picks Mushrooms的相关文章

HDU 4422 The Little Girl who Picks Mushrooms(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422 Problem Description 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 mus

状态压缩 UVALive 6068 The Little Girl who Picks Mushrooms (12长春C)

题目传送门 题意:采蘑菇.现在采了n座山,共5座山,最后要求有三个篮子的蘑菇量是1024的整数倍,丢掉后一直减1024直到不超过1024 分析:n <= 3时直接1024,否则状压枚举哪三个篮子丢弃,更新最值 /************************************************ * Author :Running_Time * Created Time :2015/10/28 星期三 19:21:49 * File Name :C.cpp *************

HDU 4422 The Little Girl who Picks Mushrooms (2012年成都赛区现场赛C题)

1.题目描述:点击打开链接 2.解题思路:本题是一道简单模拟题,然而,自己的方法不对WA了很多次==.最后不得不弃用自己的思路了.首先用-1表示还没有使用过的位置.可以每次枚举3个位置,如果发现这3个位置中没有-1且他们的和不能被1024整除,那么return 0,否则,找到没有被标记的另外2个位置,如果这2个位置其中一个为-1,那么直接返回1024,因为我们已经交了三个袋子了,剩下的袋子中如果又没有确定的,那么一定可以凑成1024.否则,计算这2个位置的和,然后看减去若干次1024后剩下多少,

zoj 3657 策略题 容易

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4880 因为是要去牡丹江,是浙大出题,所以找了份浙大的题,第一道水题做的就不顺啊,题看不明白,然后枚举3个数的组合,循环条件居然写错,二逼啊,这到现场肯定悲剧啊 题意: 一共有5座山,有人拿5个篮子去采蘑菇,现在他已经采了几座山上的蘑菇,之后几座山的蘑菇数量你可以自己确定.但是他要交出3个篮子,且它们的和必须是1024的倍数.否则,剩余两个篮子也要交出.之后,如果剩余数量大于1

HDU - 4422-The Little Girl who Picks Mushrooms

题目链接:https://vjudge.net/problem/HDU-4422 题目大意: 自行百度 题目分析: 当n<=3的时候,易得可以得到的最多的蘑菇是1024. 当n>3时,可以进行分类讨论. 注意有两个莫名奇妙的毒点: 1.我用取模WA而用while不断相减AC 2.三个相加等于零的情况也可以通过 给出代码: 1 #include <cstdio> 2 #include <iostream> 3 #include <string> 4 #incl

2012长春赛区题解(部分)

总结起来自己还是太逗比,DP还是太弱,而DP却恰是算法思维能力的体现,现在要开始注重加强这方面的训练,遇到这类题目总是不敢想,令人担忧. Problem B ZOJ 3656 Bit Magic http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3656 分析: 总共N个数,每个b[i][j]会对a[i]和a[j]的对应二进制位产生一定影响,具体见题目,我们需要做的是判断每个数的每个位是0或1,根据关系建立边,然后直接2sa

STL 全排列

The Little Girl who Picks Mushrooms Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1805    Accepted Submission(s): 579 Problem Description It's yet another festival season in Gensokyo. Little

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an